Java OCA OCP Practice Question 1490

Question

How many lines of output does the following generate?

public class Main { 
   static { //from   w w  w. j  av a2s .  c  om
      System.out.println("any"); 
   } 
   { 
      System.out.println("more"); 
   } 
   public static void main(String[] args) { 
      new Main(); 
      new Main(); 
   } 
} 
  • A. Two
  • B. Three
  • C. Four
  • D. None of the above. The code does not compile.


B.

Note

The static initializer only runs once since statics are shared by all instances.

The instance initializer runs twice because we call the constructor twice.

Therefore, Option B is correct.




PreviousNext

Related