0
0
Javaprogramming~20 mins

Constructor chaining in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of constructor chaining with this()
What is the output of the following Java program that uses constructor chaining?
Java
public class Test {
    public Test() {
        this(5);
        System.out.print("A");
    }
    public Test(int x) {
        System.out.print("B");
    }
    public static void main(String[] args) {
        new Test();
    }
}
ABA
BAB
CA
DB
Attempts:
2 left
πŸ’‘ Hint
Remember that this() calls another constructor before executing the current one.
❓ Predict Output
intermediate
2:00remaining
Constructor chaining with super() and this()
What will be printed when the following Java code runs?
Java
class Parent {
    Parent() {
        System.out.print("P");
    }
}
class Child extends Parent {
    Child() {
        this(10);
        System.out.print("C");
    }
    Child(int x) {
        super();
        System.out.print("X");
    }
}
public class Main {
    public static void main(String[] args) {
        new Child();
    }
}
AXP
BPCX
CPC
DPXC
Attempts:
2 left
πŸ’‘ Hint
Check the order of constructor calls: this() calls another constructor, which calls super().
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error in constructor chaining
Which option correctly identifies the compilation error in this Java class?
Java
public class Demo {
    Demo() {
        this(5);
        System.out.println("Hello");
    }
    Demo(int x) {
        this();
        System.out.println(x);
    }
}
ACannot call this() and super() in the same constructor
BMissing return statement in constructor
CRecursive constructor call causing a compile-time error
DNo default constructor defined
Attempts:
2 left
πŸ’‘ Hint
Check if constructors call each other in a loop.
πŸ“ Syntax
advanced
2:00remaining
Which constructor chaining syntax is invalid?
Which of the following constructor chaining statements will cause a compilation error in Java?
Apublic MyClass() { super(); this(10); }
Bpublic MyClass() { this(10); }
Cpublic MyClass(int x) { super(); }
Dpublic MyClass(int x) { this(); }
Attempts:
2 left
πŸ’‘ Hint
Remember that this() or super() must be the first statement in a constructor.
πŸš€ Application
expert
3:00remaining
Number of times constructors are called in chaining
Given the following Java classes, how many times is the constructor of class A called when creating new C()?
Java
class A {
    A() {
        System.out.print("A");
    }
}
class B extends A {
    B() {
        this(5);
        System.out.print("B");
    }
    B(int x) {
        super();
        System.out.print(x);
    }
}
class C extends B {
    C() {
        super();
        System.out.print("C");
    }
}
public class Main {
    public static void main(String[] args) {
        new C();
    }
}
A0
B1
C3
D2
Attempts:
2 left
πŸ’‘ Hint
Trace constructor calls from C() up to A(), count how many times A() runs.