0
0
Javaprogramming~10 mins

Default 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 define a default constructor for the class.

Java
public class Car {
    String model;
    int year;

    public [1]() {
        model = "Unknown";
        year = 0;
    }
}
Drag options to blanks, or click blank then click option'
ADefaultConstructor
Bcar
CCar
DCar()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase class name for constructor
Adding parentheses after the constructor name in the declaration
Using a different name than the class
2fill in blank
medium

Complete the code to create an object using the default constructor.

Java
Car myCar = new [1]();
Drag options to blanks, or click blank then click option'
Acar
BCar
CMyCar
Dcar()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase class name
Adding parentheses in the option name
Using a variable name instead of class name
3fill in blank
hard

Fix the error in the constructor declaration.

Java
public class Book {
    String title;

    public [1]() {
        title = "Unknown";
    }
}
Drag options to blanks, or click blank then click option'
ABook
BBook()
Cbook
Dvoid Book
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding 'void' before constructor name
Using lowercase constructor name
Adding parentheses in the constructor name in declaration
4fill in blank
hard

Fill both blanks to complete the default constructor and initialize the fields.

Java
public class Student {
    String name;
    int age;

    public [1]() {
        name = [2];
        age = 0;
    }
}
Drag options to blanks, or click blank then click option'
AStudent
B"Unknown"
C"Default"
Dstudent
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase constructor name
Assigning string without quotes
Using wrong string value
5fill in blank
hard

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

Java
public class Laptop {
    String brand;
    int price;

    public [1]() {
        brand = [2];
        price = [3];
    }
}

Laptop myLaptop = new [4]();
Drag options to blanks, or click blank then click option'
ALaptop
B"Generic"
C1000
Dlaptop
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using lowercase class name
Missing quotes around string
Using string for price instead of number