0
0
Javaprogramming~20 mins

Constructor overloading in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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();
    }
}
A
Volume: 0
Volume: 0
B
Volume: 0
Volume: 24
C
Volume: 1
Volume: 9
D
Volume: 1
Volume: 24
Attempts:
2 left
πŸ’‘ Hint
Check how the default constructor sets all dimensions to 1.
❓ Predict Output
intermediate
2: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");
    }
}
AConstructor with name called
BConstructor with name and age called
CDefault constructor called
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Look at the constructor signatures and which matches the argument.
πŸ”§ Debug
advanced
2: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;
    }
}
ACannot overload constructors with different parameter counts
BMissing return type in constructors
CDuplicate constructor with same parameter types
DConstructor names do not match class name
Attempts:
2 left
πŸ’‘ Hint
Check if any constructors have identical parameter lists.
❓ Predict Output
advanced
2: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();
    }
}
A
No-arg constructor
One-arg constructor
Two-arg constructor
B
Two-arg constructor
One-arg constructor
No-arg constructor
C
One-arg constructor
Two-arg constructor
No-arg constructor
DCompilation error due to constructor chaining
Attempts:
2 left
πŸ’‘ Hint
Remember that this() calls must be the first statement in a constructor.
🧠 Conceptual
expert
2:00remaining
Why constructor overloading improves code design?
Which of the following best explains why constructor overloading is useful in Java?
AIt enables creating objects with different initial data by providing multiple constructor options.
BIt allows creating multiple constructors with the same parameters but different names.
CIt forces all constructors to have the same number of parameters for consistency.
DIt automatically generates default constructors if none are defined.
Attempts:
2 left
πŸ’‘ Hint
Think about how constructors help initialize objects in different ways.