Java OCA OCP Practice Question 494

Question

Which statement is true about this code?

 1. class MyClass 
 2. { /*from   w w w  .j  a v  a  2s . c  om*/
 3.     private static int x = 100; 
 4. 
 5.     public static void main(String args[]) 
 6.     { 
 7.         MyClass hs1 = new MyClass(); 
 8.         hs1.x++; 
 9.         MyClass hs2 = new MyClass(); 
10.         hs2.x++; 
11.         hs1 = new MyClass(); 
12.         hs1.x++; 
13.         MyClass.x++; 
14.         System.out.println("x = " + x); 
15.    } 
16. } 
  • A. Line 8 will not compile because it is a static reference to a private variable.
  • B. Line 13 will not compile because it is a static reference to a private variable.
  • C. The program compiles and the output is x = 102.
  • D. The program compiles and the output is x = 103.
  • E. The program compiles and the output is x = 104.


E.

Note

The program compiles fine.

The static variable x gets incremented four times, on lines 8, 10, 12, and 13.




PreviousNext

Related