Java OCA OCP Practice Question 1486

Question

How many lines does the following code output?

public class Main { 
   private static void drive() { 
       static { /*w  w  w . j a  v a 2s.  co m*/
          System.out.println("static"); 
       } 
       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.


D.

Note

A static initializer is not allowed inside of a method.

It should go on the class level rather than the method level.

The code does not compile, and Option D is correct.




PreviousNext

Related