Java OCA OCP Practice Question 2170

Question

What is the output of the following?.

public class Main {
   {/*  www  .  jav a  2s . co m*/
      System.out.print("1");
   }
   static {
      System.out.print("2");
   }

   public Main() {
      System.out.print("3");
   }

   public static void callMe() {
      System.out.print("4");
   }

   public static void main(String[] args) {
      callMe();
      callMe();
      System.out.print("5");
   }
}
  • A. 1223445
  • B. 2445
  • C. 22445
  • D. 223445
  • E. 2233445
  • F. None of the above


B.

Note

This class is never instantiated, so the instance initializer never outputs 1 and the constructor never outputs 3.

This rules out Options A, D, and E.

A static initializer only runs once for the class, which rules out Option C.

Option B is correct because the static initializer runs once printing 2, followed by the static method callMe() printing 4 twice, and ending with the main() method printing 5.




PreviousNext

Related