Java OCA OCP Practice Question 1962

Question

Which expression can be inserted at (1) so that compiling and running the program will print LocalVar.v?

public class Main {
  final String v = "Main.v";

  public static void main(final String args[]) {
    final String v = "LocalVar.v";

    class MyClass { String getStr1() { return v; } }
    class Inner {
      String v = "Inner.v";
      Inner() {/*from   ww  w  . ja  v  a2 s.c  o  m*/
        System.out.println( /* (1) INSERT EXPRESSION HERE */ );
      }
    }
    Inner inner = new Inner();
  }
}

Select the one correct answer.

(a)  v
(b)  this.v
(c)  Main.this.v
(d)  new MyClass().getStr1()
(e)  this.new MyClass().getStr1()
(f) Main.new MyClass().getStr1()
(g) new Main.MyClass().getStr1()
(h) new Main().new MyClass().getStr1()


(d)

Note

Note that the nested classes are locally declared in a static context.

(a) and (b) refer to the field v in Inner.

(c) refers to the field v in Main.

(e) requires the MyClass class to be in the Inner class in order to compile, but this will not print the right answer.

(f), (g), and (h) will not compile, as the MyClass local class cannot be accessed using the enclosing class name.




PreviousNext

Related