Java OCA OCP Practice Question 2360

Question

What is the output of the following code?

class Base {/*from  w w  w  .  j  av  a  2  s  .  c  o m*/
    static {
        System.out.print("STATIC:");
    }
    {
        System.out.print("INIT:");
    }
}
class MyClass extends Base {
    static {
        System.out.print("Here:");
    }
    {
        System.out.print("There:");
    }
    public static void main(String args[]) {
        new MyClass();
    }
}
  • a STATIC:INIT:Here:There:
  • b INIT:STATIC:There:Here:
  • c STATIC:Here:INIT:There:
  • d Here:There:STATIC:INIT:


c

Note

When you instantiate a derived class, the derived class instantiates its base class.

The static initializers execute when a class is loaded in memory.

So the order of execution of static and instance initializer blocks is as follows:.

  • 1) Base class static initializer block
  • 2) Derived class static initializer block
  • 3) Base class instance initializer block
  • 4) Derived class instance initializer block



PreviousNext

Related