Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class named Car.
Java
public class [1] { // class body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using lowercase names for class names.
Using a different name than the one intended.
β Incorrect
The class name should be Car to match the declaration.
2fill in blank
mediumComplete the code to create an object of class Car named myCar.
Java
Car [1] = new Car(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using the class name as the variable name.
Using a name not mentioned in the instruction.
β Incorrect
The variable name for the object should be myCar as specified.
3fill in blank
hardFix the error in the constructor declaration of class Car.
Java
public class Car { public [1]() { System.out.println("Car created"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using lowercase or different names for the constructor.
β Incorrect
Constructors have no return type and must have the same name as the class, here Car.
4fill in blank
hardFill both blanks to define a method that returns the car's model name.
Java
public class Car { private String model; public [1] [2]() { return model; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'int' as return type for a String variable.
Naming the method 'setModel' instead of 'getModel'.
β Incorrect
The method should return a String and be named getModel to follow naming conventions.
5fill in blank
hardFill all three blanks to create a constructor that sets the model name.
Java
public class Car { private String model; public [1]([2] model) { this.[3] = model; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using wrong constructor name.
Using wrong parameter type.
Assigning to a wrong field name.
β Incorrect
The constructor name is Car, it takes a String parameter named model, and assigns it to the field this.model.