Java OCA OCP Practice Question 746

Question

Consider the classes shown below :

class A {/*from  w ww.j  a  v a 2  s  . co m*/
   public A() {
   }

   public A(int i) {
      System.out.println(i);
   }
}

class B {
   static A s1 = new A(1);
   A a = new A(2);

   public static void main(String[] args) {
      B b = new B();
      A a = new A(3);
   }

   static A s2 = new A(4);
}

Which is the correct sequence of the digits that will be printed when B is run?

Select 1 option

  • A. 1 ,2 ,3 4.
  • B. 1 ,4, 2 ,3
  • C. 3, 1, 2, 4
  • D. 2, 1, 4, 3
  • E. 2, 3, 1, 4


Correct Option is  : B

Note

The order of initialization of a class is:

  • All static constants, variables and blocks.
  • All non static constants, variables and blocks.
  • Constructor.



PreviousNext

Related