Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
β Incorrect
In Java, to call another constructor in the same class, use 'this()'.
2fill in blank
mediumComplete 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'
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.
β Incorrect
Use 'this(length, width);' to call the two-parameter constructor from the three-parameter constructor.
3fill in blank
hardFix 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'
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.
β Incorrect
Use 'this.name' to refer to the instance variable 'name' inside the constructor.
4fill in blank
hardFill 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'
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.
β Incorrect
Use 'this(length, width);' to call the two-parameter constructor and 'this.area = area;' to set the area.
5fill in blank
hardFill 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'
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.
β Incorrect
Use 'this(name);' to call the single-parameter constructor, 'this(name, id);' to call the two-parameter constructor, and 'this.salary = salary;' to set the salary.