0
0
Javaprogramming~10 mins

Why constructors are needed in Java - Test Your Understanding

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

Complete the code to declare a constructor for the class.

Java
public class Car {
    String model;
    public [1](String model) {
        this.model = model;
    }
}
Drag options to blanks, or click blank then click option'
Apublic Car()
Bcar
Cvoid Car
DCar
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a lowercase name for the constructor.
Adding a return type like void.
2fill in blank
medium

Complete the code to create an object using the constructor.

Java
Car myCar = new [1]("Toyota");
Drag options to blanks, or click blank then click option'
AVehicle
Bcar
CCar
DMyCar
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a lowercase class name.
Using a different class name.
3fill in blank
hard

Fix the error in the constructor declaration.

Java
public class Book {
    String title;
    public void [1](String title) {
        this.title = title;
    }
}
Drag options to blanks, or click blank then click option'
ABook()
BBook
Cvoid Book
Dbook
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using lowercase for the constructor name.
4fill in blank
hard

Fill both blanks to complete the constructor and initialize the field.

Java
public class Student {
    int age;
    public [1](int [2]) {
        this.age = age;
    }
}
Drag options to blanks, or click blank then click option'
AStudent
Bstudent
Cage
Dyears
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase constructor name.
Using a different parameter name than the field.
5fill in blank
hard

Fill all three blanks to create a constructor that initializes two fields.

Java
public class Laptop {
    String brand;
    int price;
    public [1](String [2], int [3]) {
        this.brand = brand;
        this.price = price;
    }
}
Drag options to blanks, or click blank then click option'
ALaptop
Bbrand
Cprice
Dlaptop
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase constructor name.
Using wrong parameter names.