Java OCA OCP Practice Question 492

Question

Which statement is true about this application?

 1. class MyClass 
 2  { /*from w w  w .j  a va2  s.  c o  m*/
 3.     static int x = 10; 
 4. 
 5.     static { x += 5; } 
 6. 
 7.     public static void main(String args[]) 
 8.     { 
 9.         System.out.println("x = " + x); 
10.     } 
11. 
12.     static {x /= 5; } 
13. } 
  • A. Lines 5 and 12 will not compile because the method names and return types are missing.
  • B. Line 12 will not compile because you can only have one static initializer.
  • C. The code compiles and execution produces the output x = 10.
  • D. The code compiles and execution produces the output x = 15.
  • E. The code compiles and execution produces the output x = 3.


E.

Note

Multiple static initializers (lines 5 and 12) are legal.

All static initializer code is executed at class-load time.

Before main() is ever run, the value of x is initialized to 10 (line 3), then bumped to 15 (line 5), and then divided by 5 (line 12).




PreviousNext

Related