Challenge - 5 Problems
Constructor Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of overloaded constructors with different parameters
What is the output of this Java program using constructor overloading?
Java
public class Box { int width, height, depth; Box() { width = height = depth = 1; } Box(int w, int h, int d) { width = w; height = h; depth = d; } void display() { System.out.println("Volume: " + (width * height * depth)); } public static void main(String[] args) { Box b1 = new Box(); Box b2 = new Box(2, 3, 4); b1.display(); b2.display(); } }
Attempts:
2 left
π‘ Hint
Check how the default constructor sets all dimensions to 1.
β Incorrect
The default constructor sets width, height, and depth to 1, so volume is 1*1*1=1. The overloaded constructor sets them to 2,3,4, so volume is 24.
β Predict Output
intermediate2:00remaining
Which constructor is called?
Given this Java class with overloaded constructors, what will be printed when creating an object with new Person("Alice")?
Java
public class Person { String name; int age; Person() { name = "Unknown"; age = 0; System.out.println("Default constructor called"); } Person(String n) { name = n; age = 18; System.out.println("Constructor with name called"); } Person(String n, int a) { name = n; age = a; System.out.println("Constructor with name and age called"); } public static void main(String[] args) { Person p = new Person("Alice"); } }
Attempts:
2 left
π‘ Hint
Look at the constructor signatures and which matches the argument.
β Incorrect
The constructor Person(String n) matches the call new Person("Alice"), so it prints "Constructor with name called".
π§ Debug
advanced2:00remaining
Identify the compilation error in constructor overloading
Why does this Java class fail to compile?
Java
public class Car { String model; int year; Car(String m) { model = m; } Car(String m, int y) { model = m; year = y; } Car(String m, int y) { model = m; year = y + 1; } }
Attempts:
2 left
π‘ Hint
Check if any constructors have identical parameter lists.
β Incorrect
Two constructors have the same parameter types (String, int), which causes a duplicate method error.
β Predict Output
advanced2:00remaining
Output of constructor chaining with this()
What is the output of this Java program that uses constructor chaining?
Java
public class Animal { String type; int legs; Animal() { this("Unknown"); System.out.println("No-arg constructor"); } Animal(String t) { this(t, 4); System.out.println("One-arg constructor"); } Animal(String t, int l) { type = t; legs = l; System.out.println("Two-arg constructor"); } public static void main(String[] args) { Animal a = new Animal(); } }
Attempts:
2 left
π‘ Hint
Remember that this() calls must be the first statement in a constructor.
β Incorrect
The no-arg constructor calls the one-arg constructor, which calls the two-arg constructor. The print statements execute after each call returns, so the order is two-arg, one-arg, no-arg.
π§ Conceptual
expert2:00remaining
Why constructor overloading improves code design?
Which of the following best explains why constructor overloading is useful in Java?
Attempts:
2 left
π‘ Hint
Think about how constructors help initialize objects in different ways.
β Incorrect
Constructor overloading lets you create objects with different sets of initial data by defining multiple constructors with different parameter lists.