Java OCA OCP Practice Question 1569

Question

What is the output of the following application?

package mypkg;//from   w w  w  . j a  v a 2s  . c o m
public class Main {
   protected static int first = 2;
   private final short ID = 10;
   private static class MyClass {
      int first = 5;
      static int second = ID;
   }
   private MyClass go = new MyClass();
   public static void main(String[] begin) {
      Main r = new Main();
      System.out.print(r.go.first);
      System.out.print(", "+r.go.second);
   }
}
  • A. 2, 5
  • B. 5, 10
  • C. The code does not compile because of the MyClass class body.
  • D. The code does not compile for another reason.


C.

Note

The code does not compile, so Options A and B are incorrect.

The ID is an instance variable, not a static variable; therefore, the static nested class MyClass cannot access it without a reference to the class.

The declaration of the static nested class MyClass does not compile, and Option C is the correct answer.

The rest of the code compiles without issue.

If the ID variable was modified to be static, then the code would compile without issue, and Option B would be the correct answer.




PreviousNext

Related