Java OCA OCP Practice Question 2979

Question

What will be the output of the following program?

class Base {/*from ww  w  . j a  v  a  2  s.  com*/
    public Base() {
        System.out.println("Base");
    }
}

class Derived extends Base {
    public Derived() {
        System.out.println("Derived");
    }
}

class DeriDerived extends Derived {
    public DeriDerived() {
        System.out.println("DeriDerived");
    }
}

class Main {
    public static void main(String []args) {
        Derived b = new DeriDerived();
    }
}
a)   Base/*  w  w w  . j  av  a  2s. co m*/

     Derived

     DeriDerived

b)   Derived

     DeriDerived

c)   DeriDerived

     Derived

     Base

d)   DeriDerived

     Derived

e)   DeriDerived


a)

Note

Whenever a class gets instantiated, the constructor of its base classes (the constructor of the root of the hierarchy gets executed first) gets invoked before the constructor of the instantiated class.




PreviousNext

Related