Challenge - 5 Problems
Constructor Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Remember that this() calls another constructor before executing the current one.
β Incorrect
The no-argument constructor calls the constructor with int parameter first, printing 'B'. Then it prints 'A'. So the output is 'BA'.
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Check the order of constructor calls: this() calls another constructor, which calls super().
β Incorrect
Child() calls this(10), which calls super() printing 'P', then prints 'X'. After returning, Child() prints 'C'. So output is 'PXC'.
π§ Debug
advanced2: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); } }
Attempts:
2 left
π‘ Hint
Check if constructors call each other in a loop.
β Incorrect
The constructor Demo() calls Demo(int), which calls Demo() again, causing infinite recursion in constructor calls. This is a compile-time error.
π Syntax
advanced2:00remaining
Which constructor chaining syntax is invalid?
Which of the following constructor chaining statements will cause a compilation error in Java?
Attempts:
2 left
π‘ Hint
Remember that this() or super() must be the first statement in a constructor.
β Incorrect
In option A, super() is called first, then this(10), which is invalid because this() or super() must be the first statement and only one can be called.
π Application
expert3: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(); } }
Attempts:
2 left
π‘ Hint
Trace constructor calls from C() up to A(), count how many times A() runs.
β Incorrect
Creating new C() calls C(), which calls B() no-arg, which calls B(int) via this(5). B(int) calls super() which calls A() once. So A() is called once.