Java OCA OCP Practice Question 2774

Question

Given:

3. public class Main {  
4.   private int x = 2;  
5.   protected int y = 3;  
6.   private static int m1 = 4;  
7.   protected static int m2 = 5;  
8.   public static void main(String[] args) {  
9.      int x = 6;   int y = 7;  
10.     int m1 = 8;  int m2 = 9;  
11.     new Main().new Secret().go();  
12.   }  //from w ww  . ja  v a2s .com
13.   class Secret {  
14.     void go() { System.out.println(x + " " + y + " " + m1 + " " + m2); }  
15. } } 

What is the result?

  • A. 2 3 4 5
  • B. 2 7 4 9
  • C. 6 3 8 4
  • D. 6 7 8 9
  • E. Compilation fails due to multiple errors.
  • F. Compilation fails due only to an error on line 11.
  • G. Compilation fails due only to an error on line 14.


A is correct.

Note

All of the code is legal.

The inner class has access to all of the outer class's variables (even the private ones), except for main()'s local variables.

Remember that an inner class must have an instance of the outer class to be tied to, which is why line 11 starts by creating an instance of Main.




PreviousNext

Related