Bird
Raised Fist0
Javaprogramming~10 mins

Constructor chaining in Java - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the default constructor from the parameterized constructor using constructor chaining.

Java
public class Car {
    private String model;

    public Car() {
        this.model = "Unknown";
    }

    public Car(String model) {
        [1]();
        this.model = model;
    }
}
Drag options to blanks, or click blank then click option'
ACar
Bnew Car
Cthis
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'super()' instead of 'this()' to call the constructor.
Trying to create a new object with 'new Car()' inside the constructor.
Calling the constructor by its name like a method.
2fill in blank
medium

Complete the code to chain constructors so that the three-parameter constructor calls the two-parameter constructor.

Java
public class Box {
    private int length, width, height;

    public Box(int length, int width) {
        this.length = length;
        this.width = width;
        this.height = 0;
    }

    public Box(int length, int width, int height) {
        [1](length, width);
        this.height = height;
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bsuper
CBox
Dnew Box
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'super' instead of 'this' to call the constructor.
Trying to call the constructor like a method without 'this'.
Using 'new' keyword inside the constructor.
3fill in blank
hard

Fix the error in the constructor chaining code by completing the blank.

Java
public class Person {
    private String name;
    private int age;

    public Person() {
        this("Unknown", 0);
    }

    public Person(String name, int age) {
        [1] = name;
        this.age = age;
    }
}
Drag options to blanks, or click blank then click option'
Athis.name
Bname
Csuper.name
DPerson.name
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the parameter name instead of the instance variable.
Trying to use 'super' to access instance variables.
Using the class name to access instance variables.
4fill in blank
hard

Fill both blanks to complete constructor chaining where the three-parameter constructor calls the two-parameter constructor and initializes the third field.

Java
public class Rectangle {
    private int length, width, area;

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
        this.area = length * width;
    }

    public Rectangle(int length, int width, int area) {
        [1](length, width);
        this.[2] = area;
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bsuper
Carea
Dlength
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'super' instead of 'this' to call the constructor.
Using wrong variable names in the second blank.
Not calling the constructor properly.
5fill in blank
hard

Fill all three blanks to complete constructor chaining and initialize all fields correctly.

Java
public class Employee {
    private String name;
    private int id;
    private double salary;

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

    public Employee(String name, int id) {
        [1](name);
        this.id = id;
        this.salary = 0.0;
    }

    public Employee(String name, int id, double salary) {
        [2](name, id);
        this.[3] = salary;
    }
}
Drag options to blanks, or click blank then click option'
Athis
Csalary
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'super' instead of 'this' for constructor chaining.
Not using 'this' to refer to the salary variable.
Calling constructors incorrectly.

Practice

(1/5)
1. What is the main purpose of constructor chaining in Java?
easy
A. To allow one constructor to call another constructor in the same class
B. To inherit constructors from a parent class automatically
C. To create multiple objects with the same constructor
D. To override constructors with different names

Solution

  1. Step 1: Understand constructor chaining concept

    Constructor chaining means one constructor calls another constructor in the same class to reuse code.
  2. Step 2: Identify the correct purpose

    To allow one constructor to call another constructor in the same class correctly describes this behavior using this(...) to call another constructor.
  3. Final Answer:

    To allow one constructor to call another constructor in the same class -> Option A
  4. Quick Check:

    Constructor chaining = calling another constructor [OK]
Hint: Constructor chaining uses this(...) to call another constructor [OK]
Common Mistakes:
  • Confusing constructor chaining with inheritance
  • Thinking constructors can have different names
  • Believing constructor chaining creates multiple objects
2. Which of the following is the correct syntax to chain constructors in Java?
easy
A. this.call(); // calls another method
B. super(); // calls constructor of same class
C. this(); // must be the first statement in constructor
D. constructor(); // calls constructor by name

Solution

  1. Step 1: Recall syntax for constructor chaining

    Constructor chaining uses this(...) as the first statement inside a constructor.
  2. Step 2: Identify correct option

    this(); // must be the first statement in constructor shows this(); which is the correct syntax to call another constructor in the same class.
  3. Final Answer:

    this(); // must be the first statement in constructor -> Option C
  4. Quick Check:

    Constructor chaining syntax = this() first [OK]
Hint: Use this() as first line to chain constructors [OK]
Common Mistakes:
  • Using super() to call same class constructor
  • Calling constructor by its name directly
  • Placing this() after other statements
3. What will be the output of the following Java code?
class Test {
  Test() {
    this(5);
    System.out.print("A");
  }
  Test(int x) {
    System.out.print("B");
  }
  public static void main(String[] args) {
    new Test();
  }
}
medium
A. AB
B. BA
C. A
D. B

Solution

  1. Step 1: Trace constructor calls

    Creating new Test() calls the no-arg constructor, which calls Test(int x) first (prints "B"), then prints "A".
  2. Step 2: Determine output order

    Since Test(int x) prints "B" first, then control returns to no-arg constructor which prints "A", output is "BA".
  3. Final Answer:

    BA -> Option B
  4. Quick Check:

    Constructor chaining prints B then A [OK]
Hint: Constructor chaining prints inner constructor output first [OK]
Common Mistakes:
  • Assuming outer constructor prints before inner
  • Confusing order of constructor calls
  • Ignoring that this() must be first line
4. Identify the error in the following Java code related to constructor chaining:
class Sample {
  Sample() {
    System.out.println("Hello");
    this(10);
  }
  Sample(int x) {
    System.out.println(x);
  }
}
medium
A. Constructor chaining call this(10) must be the first statement
B. Constructor name must match class name
C. Cannot overload constructors with different parameters
D. Missing return type in constructors

Solution

  1. Step 1: Check constructor chaining rules

    In Java, the call to another constructor using this(...) must be the first statement in the constructor.
  2. Step 2: Identify the error

    Here, System.out.println("Hello") comes before this(10);, which violates the rule.
  3. Final Answer:

    Constructor chaining call this(10) must be the first statement -> Option A
  4. Quick Check:

    this() must be first line in constructor [OK]
Hint: this() call must be first statement in constructor [OK]
Common Mistakes:
  • Placing code before this() call
  • Confusing constructor overloading with chaining
  • Thinking constructors need return types
5. Given the class below, what will be the output when new Box(); is executed?
class Box {
  Box() {
    this(3, 4);
    System.out.print("X");
  }
  Box(int w, int h) {
    this(w, h, 5);
    System.out.print("Y");
  }
  Box(int w, int h, int d) {
    System.out.print("Z");
  }
}
hard
A. ZXY
B. ZY
C. XYZ
D. ZYX

Solution

  1. Step 1: Trace constructor chaining calls

    Calling new Box() calls no-arg constructor, which calls Box(int w, int h), which calls Box(int w, int h, int d).
  2. Step 2: Track printed characters in order

    The deepest constructor prints "Z" first, then returns to middle constructor which prints "Y", then returns to no-arg constructor which prints "X".
  3. Final Answer:

    ZYX -> Option D
  4. Quick Check:

    Chained constructors print Z then Y then X [OK]
Hint: Deepest constructor prints first, then unwind chain [OK]
Common Mistakes:
  • Assuming outer constructor prints first
  • Ignoring chaining order
  • Mixing up print order in nested calls