Bird
Raised Fist0
Javaprogramming~10 mins

Default constructor in Java - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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

Practice

(1/5)
1. What is a default constructor in Java?
easy
A. A method that returns the default value of a class.
B. A constructor with no parameters that Java provides automatically if none is written.
C. A constructor that must always be written by the programmer.
D. A special method that runs only when a program ends.

Solution

  1. Step 1: Understand what a constructor is

    A constructor is a special method used to create objects of a class.
  2. Step 2: Identify the default constructor

    If no constructor is written, Java automatically provides a constructor with no parameters called the default constructor.
  3. Final Answer:

    A constructor with no parameters that Java provides automatically if none is written. -> Option B
  4. Quick Check:

    Default constructor = automatic no-parameter constructor [OK]
Hint: Default constructor has no parameters and is auto-created if missing [OK]
Common Mistakes:
  • Thinking default constructor must be written manually
  • Confusing default constructor with methods returning default values
  • Believing default constructor runs at program end
2. Which of the following is the correct syntax for a default constructor in Java?
easy
A. public ClassName(void) { }
B. public void ClassName() { }
C. public ClassName() { }
D. void ClassName() { }

Solution

  1. Step 1: Recall constructor syntax

    A constructor has the same name as the class and no return type.
  2. Step 2: Check each option

    public ClassName() { } matches the class name and has no return type, so it's correct syntax.
  3. Final Answer:

    public ClassName() { } -> Option C
  4. Quick Check:

    Constructor syntax = class name + no return type [OK]
Hint: Constructor name = class name, no return type, parentheses empty [OK]
Common Mistakes:
  • Adding void return type to constructor
  • Using wrong parameter list syntax
  • Using lowercase class name in constructor
3. What will be the output of this Java code?
class Car {
  String model;
}
public class Test {
  public static void main(String[] args) {
    Car c = new Car();
    System.out.println(c.model);
  }
}
medium
A. null
B. Empty string
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand default constructor usage

    No constructor is defined, so Java provides a default constructor that sets no initial values.
  2. Step 2: Check default value of uninitialized String

    Instance variable 'model' is a String and defaults to null if not set.
  3. Final Answer:

    null -> Option A
  4. Quick Check:

    Uninitialized String = null by default [OK]
Hint: Uninitialized object fields default to null in Java [OK]
Common Mistakes:
  • Expecting empty string instead of null
  • Thinking default constructor sets values automatically
  • Assuming compilation or runtime error
4. Identify the error in this Java class if any:
public class Book {
  String title;
  public Book() {
    title = "Java Basics";
  }
  public Book(String title) {
    title = title;
  }
}
medium
A. The second constructor does not set the instance variable correctly.
B. The default constructor is missing.
C. The class has no constructors.
D. The constructors have wrong return types.

Solution

  1. Step 1: Analyze the second constructor

    It assigns parameter 'title' to itself, not to the instance variable.
  2. Step 2: Understand correct assignment

    Use 'this.title = title;' to assign parameter to instance variable.
  3. Final Answer:

    The second constructor does not set the instance variable correctly. -> Option A
  4. Quick Check:

    Use 'this' to assign constructor parameters to fields [OK]
Hint: Use 'this.' to assign parameters to instance variables [OK]
Common Mistakes:
  • Assigning parameter to itself instead of instance variable
  • Missing default constructor (actually present)
  • Adding return types to constructors
5. You want to create a class Person that sets the name to "Unknown" by default if no name is given. Which constructor code correctly implements this using a default constructor?
hard
A. public class Person { String name; public Person() { name = name; } }
B. public class Person { String name = "Unknown"; public Person(String name) { name = name; } }
C. public class Person { String name; public Person(String name) { this.name = name; } }
D. public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } }

Solution

  1. Step 1: Check default constructor sets default value

    public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } }'s default constructor sets name to "Unknown" correctly.
  2. Step 2: Verify parameterized constructor sets name properly

    public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } } uses 'this.name = name;' to assign parameter to instance variable.
  3. Final Answer:

    public class Person { String name; public Person() { name = "Unknown"; } public Person(String name) { this.name = name; } } -> Option D
  4. Quick Check:

    Default constructor sets default value, parameterized sets given value [OK]
Hint: Default constructor sets default values; use 'this' for parameters [OK]
Common Mistakes:
  • Assigning parameter to itself without 'this.'
  • Not setting default value in default constructor
  • Missing default constructor entirely