0
0
Javaprogramming~20 mins

Constructor execution flow in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this constructor execution?

Consider the following Java code with inheritance and constructors. What will be printed when new Child() is executed?

Java
class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}
A
Parent constructor
Child constructor
B
Child constructor
Parent constructor
CChild constructor
DParent constructor
Attempts:
2 left
πŸ’‘ Hint

Remember that the parent constructor runs before the child constructor.

❓ Predict Output
intermediate
2:00remaining
What is the output when constructors call each other?

Analyze the constructor calls and determine the output.

Java
class A {
    A() {
        this(5);
        System.out.println("A no-arg");
    }
    A(int x) {
        System.out.println("A int: " + x);
    }
}

public class Test {
    public static void main(String[] args) {
        new A();
    }
}
A
A no-arg
A int: 5
B
A int: 5
A no-arg
CA int: 5
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Look at how this() calls another constructor before printing.

πŸ”§ Debug
advanced
2:00remaining
What error occurs in this constructor chaining?

Examine the code and identify the error that occurs when compiling.

Java
class B {
    B() {
        this();
        System.out.println("B no-arg");
    }
}

public class Test {
    public static void main(String[] args) {
        new B();
    }
}
ANullPointerException at runtime
BStackOverflowError at runtime
CNo output, program runs silently
DCompilation error: recursive constructor call
Attempts:
2 left
πŸ’‘ Hint

Check how this() is used inside the constructor.

❓ Predict Output
advanced
2:00remaining
What is the output of this multi-level constructor chain?

Given the classes below, what will be printed when new C() is executed?

Java
class A {
    A() {
        System.out.println("A constructor");
    }
}

class B extends A {
    B() {
        System.out.println("B constructor");
    }
}

class C extends B {
    C() {
        super();
        System.out.println("C constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new C();
    }
}
A
A constructor
B constructor
C constructor
B
B constructor
A constructor
C constructor
CCompilation error
D
A constructor
C constructor
B constructor
Attempts:
2 left
πŸ’‘ Hint

Remember the order of constructor calls in inheritance.

🧠 Conceptual
expert
2:00remaining
How many times is the constructor of class A called?

Consider the following code. How many times is the constructor of class A executed when new D() is called?

Java
class A {
    A() {
        System.out.println("A constructor");
    }
}

class B extends A {
    B() {
        System.out.println("B constructor");
    }
}

class C extends A {
    C() {
        System.out.println("C constructor");
    }
}

class D extends B {
    D() {
        super();
        System.out.println("D constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new D();
    }
}
A2
B3
C1
D0
Attempts:
2 left
πŸ’‘ Hint

Trace the inheritance chain and constructor calls carefully.