Bird
Raised Fist0
Javaprogramming~20 mins

Constructor overloading in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What is constructor overloading in Java?

easy
A. Having multiple constructors with different parameter lists in the same class.
B. Using the same constructor name with the same parameters multiple times.
C. Creating constructors only with no parameters.
D. Defining constructors outside the class.

Solution

  1. Step 1: Understand constructor overloading definition

    Constructor overloading means having more than one constructor in a class, each with a different set of parameters.
  2. Step 2: Check options against definition

    Having multiple constructors with different parameter lists in the same class. correctly states this. Options B, C, and D are incorrect because they describe invalid or unrelated concepts.
  3. Final Answer:

    Having multiple constructors with different parameter lists in the same class. -> Option A
  4. Quick Check:

    Constructor overloading = multiple constructors with different parameters [OK]
Hint: Look for multiple constructors with unique parameter lists [OK]
Common Mistakes:
  • Thinking constructors must have different names
  • Confusing overloading with overriding
  • Believing constructors cannot have parameters
2.

Which of the following constructor declarations is correct for overloading?

public class Car {
    public Car() { }
    public Car(String model) { }
    public Car(int year) { }
    public Car(String model, int year) { }
}
easy
A. Only constructors with one parameter are allowed.
B. Constructors must have different names, so this is incorrect.
C. All constructors have different parameter lists, so all are correct.
D. Constructors cannot have parameters, so only the first is correct.

Solution

  1. Step 1: Check parameter lists of constructors

    Each constructor has a unique parameter list: no parameters, one String, one int, and two parameters.
  2. Step 2: Verify constructor overloading rules

    Constructors can share the same name but must differ in parameters. This code follows that rule.
  3. Final Answer:

    All constructors have different parameter lists, so all are correct. -> Option C
  4. Quick Check:

    Different parameters = valid overloading [OK]
Hint: Check if parameter lists differ, not constructor names [OK]
Common Mistakes:
  • Thinking constructors need different names
  • Believing constructors cannot have parameters
  • Confusing method overloading rules with constructors
3.

What will be the output of the following code?

class Box {
    int width, height;
    Box() {
        width = 10;
        height = 10;
    }
    Box(int w, int h) {
        width = w;
        height = h;
    }
    void display() {
        System.out.println(width + "," + height);
    }
}

public class Test {
    public static void main(String[] args) {
        Box b1 = new Box();
        Box b2 = new Box(5, 15);
        b1.display();
        b2.display();
    }
}
medium
A. 10,10 5,15
B. 5,15 10,10
C. 10,10 10,10
D. Compilation error due to constructor overloading

Solution

  1. Step 1: Identify which constructors are called

    b1 uses the no-argument constructor setting width and height to 10. b2 uses the two-parameter constructor setting width=5 and height=15.
  2. Step 2: Understand display output

    b1.display() prints "10,10" and b2.display() prints "5,15" on separate lines.
  3. Final Answer:

    10,10 5,15 -> Option A
  4. Quick Check:

    Constructor sets values correctly, output matches [OK]
Hint: Match constructor parameters to object creation [OK]
Common Mistakes:
  • Assuming default constructor sets zero values
  • Mixing order of printed outputs
  • Thinking overloading causes errors here
4.

Find the error in the following code snippet:

class Person {
    String name;
    int age;
    Person(String n) {
        name = n;
    }
    Person(String n, int a) {
        name = n;
        age = a;
    }
    Person() {
        this("Unknown");
        this(0);
    }
}
medium
A. Missing return type in constructors.
B. Cannot call two constructors using 'this()' in the same constructor.
C. Constructor names must be different for overloading.
D. No error; code is correct.

Solution

  1. Step 1: Analyze constructor chaining rules

    In Java, a constructor can call only one other constructor using 'this()' and it must be the first statement.
  2. Step 2: Identify the error in the no-argument constructor

    The no-argument constructor calls 'this("Unknown")' and then 'this(0)', which is not allowed.
  3. Final Answer:

    Cannot call two constructors using 'this()' in the same constructor. -> Option B
  4. Quick Check:

    Only one 'this()' call allowed per constructor [OK]
Hint: Only one 'this()' call allowed as first line in constructor [OK]
Common Mistakes:
  • Trying to call multiple constructors with 'this()'
  • Thinking constructor names must differ
  • Forgetting 'this()' must be first statement
5.

Consider a class Employee with overloaded constructors:

class Employee {
    String name;
    int id;
    double salary;

    Employee(String name) {
        this.name = name;
        this.id = 0;
        this.salary = 0.0;
    }

    Employee(String name, int id) {
        this(name);
        this.id = id;
    }

    Employee(String name, int id, double salary) {
        this(name, id);
        this.salary = salary;
    }

    void display() {
        System.out.println(name + "," + id + "," + salary);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee e = new Employee("Alice", 101, 75000.0);
        e.display();
    }
}

What will be the output when running Main?

hard
A. Alice,101,0.0
B. Alice,0,0.0
C. Compilation error due to constructor chaining
D. Alice,101,75000.0

Solution

  1. Step 1: Trace constructor calls for Employee("Alice", 101, 75000.0)

    The three-parameter constructor calls the two-parameter constructor with ("Alice", 101), which calls the one-parameter constructor with ("Alice").
  2. Step 2: Understand field assignments

    The one-parameter constructor sets name="Alice", id=0, salary=0.0. The two-parameter constructor updates id=101. The three-parameter constructor updates salary=75000.0.
  3. Step 3: Output values

    After all calls, name="Alice", id=101, salary=75000.0, so display prints "Alice,101,75000.0".
  4. Final Answer:

    Alice,101,75000.0 -> Option D
  5. Quick Check:

    Constructor chaining updates fields stepwise [OK]
Hint: Follow constructor calls step-by-step to track field values [OK]
Common Mistakes:
  • Assuming fields reset after each constructor call
  • Thinking constructor chaining causes errors
  • Ignoring order of field assignments