0
0
Javaprogramming~10 mins

Classes and objects in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class named Car.

Java
public class [1] {
    // class body
}
Drag options to blanks, or click blank then click option'
Acar
BCar
CVehicle
DcarClass
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase names for class names.
Using a different name than the one intended.
2fill in blank
medium

Complete 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'
ACar
BnewCar
CcarObject
DmyCar
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the class name as the variable name.
Using a name not mentioned in the instruction.
3fill in blank
hard

Fix 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'
ACar
Bcar
Cvoid Car
DCar()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using lowercase or different names for the constructor.
4fill in blank
hard

Fill 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'
AString
Bint
CgetModel
DsetModel
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'int' as return type for a String variable.
Naming the method 'setModel' instead of 'getModel'.
5fill in blank
hard

Fill 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'
ACar
BString
Cmodel
DcarModel
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong constructor name.
Using wrong parameter type.
Assigning to a wrong field name.