0
0
Javaprogramming~10 mins

Constructor overloading 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 define a constructor with no parameters.

Java
public class Car {
    String model;
    int year;

    public [1]() {
        model = "Unknown";
        year = 0;
    }
}
Drag options to blanks, or click blank then click option'
Acar
BCar
CCar()
Dvoid Car
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Adding a return type like 'void'.
Including parentheses in the constructor name.
2fill in blank
medium

Complete the code to define a constructor with two parameters.

Java
public class Car {
    String model;
    int year;

    public Car(String [1], int y) {
        model = [1];
        year = y;
    }
}
Drag options to blanks, or click blank then click option'
Amodel
BModel
CcarModel
Dyear
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different parameter name than the one assigned.
Using uppercase letters incorrectly.
3fill in blank
hard

Fix the error in the constructor name to correctly overload it.

Java
public class Car {
    String model;
    int year;

    public [1](String model) {
        this.model = model;
        year = 2020;
    }
}
Drag options to blanks, or click blank then click option'
Acar
Bcar()
CCAR
DCar
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase 'car' instead of 'Car'.
Adding parentheses in the constructor name.
4fill in blank
hard

Fill both blanks to create a constructor that sets model and year.

Java
public class Car {
    String model;
    int year;

    public Car([1] model, [2] year) {
        this.model = model;
        this.year = year;
    }
}
Drag options to blanks, or click blank then click option'
AString
Bint
Cdouble
Dboolean
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong data types like double or boolean.
Mixing up the order of parameters.
5fill in blank
hard

Fill all three blanks to create an overloaded constructor with default year.

Java
public class Car {
    String model;
    int year;

    public Car([1] model) {
        this.model = model;
        this.year = [2];
    }

    public Car([3] model, int year) {
        this.model = model;
        this.year = year;
    }
}
Drag options to blanks, or click blank then click option'
AString
B2023
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using int instead of String for model.
Not setting a default year value.