Java OCA OCP Practice Question 3042

Question

Given:

public class Main {
   static String s;
   static {/*from   ww w . j  av  a 2 s .  co m*/
      s = "";
   }
   {
      System.out.print("SQL ");
   }
   static {
      System.out.print(s.concat("MySQL "));
   }

   Main() {
      System.out.print(s.concat("Web "));
   }

   public static void main(String[] args) {
      new Main();
      System.out.println("Java");
   }
}

What is the result?

A.  MySQL Web Java
B.  MySQL Web SQL Java
C.  MySQL SQL Web Java
D.  Web SQL MySQL Java
E.  SQL MySQL Web Java
F.  A NullPointerException is thrown at runtime.
G.  An ExceptionInInitializationError is thrown at runtime.


C is correct.

Note

static init blocks run before instance init blocks (in the order in which they appear, respectively), and init blocks run hard on the heels of a constructor's call to super().




PreviousNext

Related