Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a default constructor for the class.
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 class name for constructor
Adding parentheses after the constructor name in the declaration
Using a different name than the class
β Incorrect
In Java, the default constructor has the same name as the class and no parameters.
2fill in blank
mediumComplete the code to create an object using the default constructor.
Java
Car myCar = new [1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase class name
Adding parentheses in the option name
Using a variable name instead of class name
β Incorrect
To create an object, use the class name with 'new' and parentheses.
3fill in blank
hardFix the error in the constructor declaration.
Java
public class Book { String title; public [1]() { title = "Unknown"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Adding 'void' before constructor name
Using lowercase constructor name
Adding parentheses in the constructor name in declaration
β Incorrect
Constructors do not have a return type and must have the same name as the class.
4fill in blank
hardFill both blanks to complete the default constructor and initialize the fields.
Java
public class Student { String name; int age; public [1]() { name = [2]; age = 0; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase constructor name
Assigning string without quotes
Using wrong string value
β Incorrect
The constructor name must match the class name, and string values must be in quotes.
5fill in blank
hardFill all three blanks to complete the class with a default constructor and create an object.
Java
public class Laptop { String brand; int price; public [1]() { brand = [2]; price = [3]; } } Laptop myLaptop = new [4]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase class name
Missing quotes around string
Using string for price instead of number
β Incorrect
Constructor name matches class name, string values use quotes, and price is an integer.