0
0
Javaprogramming~10 mins

Parameterized constructor 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 parameterized constructor for the class.

Java
public class Car {
    String model;
    int year;

    public [1](String model, int year) {
        this.model = model;
        this.year = year;
    }
}
Drag options to blanks, or click blank then click option'
ACar
Bcar
CCarConstructor
DConstructor
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different name than the class for the constructor.
Using lowercase instead of uppercase for the constructor name.
2fill in blank
medium

Complete the code to create an object of the class using the parameterized constructor.

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

Fix the error in the constructor declaration.

Java
public class Book {
    String title;
    int pages;

    public [1](String title, int pages) {
        this.title = title;
        this.pages = pages;
    }
}
Drag options to blanks, or click blank then click option'
ABook
Bbook
CsetBook
DBookConstructor
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type to the constructor.
Using a different name than the class for the constructor.
4fill in blank
hard

Fill both blanks to complete the parameterized constructor and assign values.

Java
public class Student {
    String name;
    int age;

    public [1](String name, int age) {
        this.[2] = name;
        this.age = age;
    }
}
Drag options to blanks, or click blank then click option'
AStudent
Bstudent
Cname
Dage
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase 'student' as constructor name.
Assigning to a wrong field name.
5fill in blank
hard

Fill all three blanks to complete the class with a parameterized constructor and create an object.

Java
public class Laptop {
    String brand;
    int ram;

    public [1](String brand, int ram) {
        this.brand = [2];
        this.ram = ram;
    }

    public static void main(String[] args) {
        Laptop myLaptop = new [3]("Dell", 16);
    }
}
Drag options to blanks, or click blank then click option'
ALaptop
Bbrand
Dlaptop
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase 'laptop' instead of 'Laptop'.
Assigning wrong variable to the field.