Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a constructor with no parameters.
Java
public class Car { String model; int year; public [1]() { model = "Unknown"; year = 0; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Adding a return type like 'void'.
Including parentheses in the constructor name.
β Incorrect
The constructor name must exactly match the class name, which is Car.
2fill in blank
mediumComplete the code to define a constructor with two parameters.
Java
public class Car { String model; int year; public Car(String [1], int y) { model = [1]; year = y; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different parameter name than the one assigned.
Using uppercase letters incorrectly.
β Incorrect
The parameter name should be model to match the assignment inside the constructor.
3fill in blank
hardFix the error in the constructor name to correctly overload it.
Java
public class Car { String model; int year; public [1](String model) { this.model = model; year = 2020; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Adding parentheses in the constructor name.
β Incorrect
The constructor name must exactly match the class name Car with correct capitalization.
4fill in blank
hardFill both blanks to create a constructor that sets model and year.
Java
public class Car { String model; int year; public Car([1] model, [2] year) { this.model = model; this.year = year; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using wrong data types like double or boolean.
Mixing up the order of parameters.
β Incorrect
The model is a String and year is an int.
5fill in blank
hardFill all three blanks to create an overloaded constructor with default year.
Java
public class Car { String model; int year; public Car([1] model) { this.model = model; this.year = [2]; } public Car([3] 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 int instead of String for model.
Not setting a default year value.
β Incorrect
The first constructor takes a String model and sets year to 2023. The second constructor takes a String model and an int year.