0
0
Javaprogramming~10 mins

Constructor chaining in Java - Interactive Code Practice

Choose your learning style9 modes available
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.