Java OCA OCP Practice Question 1472

Question

How many lines does the following code output?

public class Main { 
  static { /*from   w  w  w .  j  av  a  2 s .  c o m*/
     System.out.println("static"); 
  } 
  private static void drive() { 
     System.out.println("fast"); 
  } 
  public static void main(String[] args) { 
     drive(); 
     drive(); 
  } 
} 
  • A. One
  • B. Two
  • C. Three
  • D. None of the above. The code does not compile.


C.

Note

The static initializer is only run once.

The static method is run twice since it is called twice.

Three lines are printed, and Option C is correct.




PreviousNext

Related