Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a parameterized constructor for the class.
Java
public class Car { String model; int year; public [1](String model, int year) { this.model = model; this.year = year; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different name than the class for the constructor.
Using lowercase instead of uppercase for the constructor name.
β Incorrect
In Java, the constructor name must exactly match the class name, which is 'Car' here.
2fill in blank
mediumComplete the code to create an object of the class using the parameterized constructor.
Java
Car myCar = new [1]("Toyota", 2020);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Using a different class name.
β Incorrect
To create an object, use the class name 'Car' with the new keyword.
3fill in blank
hardFix the error in the constructor declaration.
Java
public class Book { String title; int pages; public [1](String title, int pages) { this.title = title; this.pages = pages; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using a different name than the class for the constructor.
β Incorrect
Constructors should not have a return type like 'void' and must be named exactly as the class.
4fill in blank
hardFill both blanks to complete the parameterized constructor and assign values.
Java
public class Student { String name; int age; public [1](String name, int age) { this.[2] = name; this.age = age; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase 'student' as constructor name.
Assigning to a wrong field name.
β Incorrect
Constructor name must match class name 'Student'. Use 'name' to assign the parameter to the field.
5fill in blank
hardFill all three blanks to complete the class with a parameterized constructor and create an object.
Java
public class Laptop { String brand; int ram; public [1](String brand, int ram) { this.brand = [2]; this.ram = ram; } public static void main(String[] args) { Laptop myLaptop = new [3]("Dell", 16); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase 'laptop' instead of 'Laptop'.
Assigning wrong variable to the field.
β Incorrect
Constructor name and object creation use class name 'Laptop'. Use parameter 'brand' to assign to field.