Java Daily, Quiz 1

Welcome to Java daily quiz 1, test yourself with this Java quiz. The answer and explanation are given at the end for your reference.
Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.

Q1 - What will be the output of the following program?

class Base {
    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");
    }
}

public class Test {
    public static void main(String[] args) {
        Derived b = new DeriDerived();
    }
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived

Answer with Explanation

Answer :
a)
Base
Derived
DeriDerived
Explanation: 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. 
You can run this program in IDE and see the output.
Check out all the Java daily quiz questions at https://www.javaguides.net/p/java-daily-quiz.html.
Let me know your feedback so that I will plan to publish such questions daily basis.

Comments

Post a Comment

Leave Comment