Bird
Raised Fist0
Javaprogramming~20 mins

Parameterized constructor in Java - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
πŸŽ–οΈ
Parameterized Constructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parameterized constructor with multiple objects
What is the output of the following Java program that uses a parameterized constructor to initialize objects?
Java
public class Car {
    String model;
    int year;

    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    public void display() {
        System.out.println(model + " " + year);
    }

    public static void main(String[] args) {
        Car car1 = new Car("Toyota", 2020);
        Car car2 = new Car("Honda", 2018);
        car1.display();
        car2.display();
    }
}
A
Toyota 2020
Honda 2018
B
Toyota 2018
Honda 2020
C
null 2020
null 2018
DCompilation error due to missing default constructor
Attempts:
2 left
πŸ’‘ Hint
Look at how the constructor assigns values to the instance variables.
❓ Predict Output
intermediate
1:30remaining
Value of instance variable after parameterized constructor call
What will be the value of the instance variable 'price' after creating the object with the parameterized constructor?
Java
public class Book {
    double price;

    public Book(double price) {
        this.price = price;
    }

    public static void main(String[] args) {
        Book b = new Book(29.99);
        System.out.println(b.price);
    }
}
ACompilation error due to missing default constructor
B0.0
Cnull
D29.99
Attempts:
2 left
πŸ’‘ Hint
The constructor sets the price variable directly.
πŸ”§ Debug
advanced
2:30remaining
Identify the error in parameterized constructor usage
What error will this code produce when compiled and run?
Java
public class Student {
    String name;
    int age;

    public Student(String name, int age) {
        name = name;
        age = age;
    }

    public void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s = new Student("Alice", 20);
        s.display();
    }
}
AOutput: Alice 20
BRuntime NullPointerException
COutput: null 0
DCompilation error: variable name is not defined
Attempts:
2 left
πŸ’‘ Hint
Check how the constructor assigns values to instance variables.
πŸ“ Syntax
advanced
2:00remaining
Which option causes a compilation error in parameterized constructor?
Which of the following constructor definitions will cause a compilation error?
Apublic Person(String name, int age) { name = name; age = age; }
Bpublic Person(String name, int age) { this.name = name; this.age = age }
Cpublic Person(String name, int age) { this.name = name; age = age; }
Dpublic Person(String name, int age) { this.name = name; this.age = age; }
Attempts:
2 left
πŸ’‘ Hint
Check for missing semicolons and correct syntax inside the constructor.
πŸš€ Application
expert
3:00remaining
Number of objects created and their state after parameterized constructor calls
Consider the following Java code. How many objects are created and what is the output when the main method runs?
Java
public class Employee {
    String name;
    int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("John", 101);
        Employee e2 = new Employee("Jane", 102);
        Employee e3 = e1;
        e3.name = "Mike";
        System.out.println(e1.name + " " + e1.id);
        System.out.println(e2.name + " " + e2.id);
    }
}
A
2 objects created; Output:
Mike 101
Jane 102
B
3 objects created; Output:
John 101
Jane 102
C
2 objects created; Output:
John 101
Jane 102
D
3 objects created; Output:
Mike 101
Jane 102
Attempts:
2 left
πŸ’‘ Hint
Remember that e3 is a reference to e1, not a new object.

Practice

(1/5)
1.

What is the main purpose of a parameterized constructor in Java?

easy
A. To initialize an object with specific values when it is created
B. To create multiple objects without any initial values
C. To delete an object from memory
D. To define a method that returns a value

Solution

  1. Step 1: Understand constructor purpose

    A constructor is used to initialize objects when they are created.
  2. Step 2: Identify parameterized constructor role

    A parameterized constructor takes arguments to set initial values for the object's fields.
  3. Final Answer:

    To initialize an object with specific values when it is created -> Option A
  4. Quick Check:

    Parameterized constructor = initialize with values [OK]
Hint: Constructors with parameters set initial object values [OK]
Common Mistakes:
  • Confusing constructors with methods
  • Thinking constructors delete objects
  • Believing constructors don't take parameters
2.

Which of the following is the correct syntax for a parameterized constructor in Java?

public class Car {
    String model;
    int year;

    // Constructor here
}
easy
A. void Car(String model, int year) { this.model = model; this.year = year; }
B. public Car(String model, int year) { this.model = model; this.year = year; }
C. public Car() { model = ""; year = 0; }
D. public void Car(String model, int year) { model = model; year = year; }

Solution

  1. Step 1: Check constructor syntax

    A constructor has no return type and matches the class name exactly.
  2. Step 2: Verify parameter usage

    public Car(String model, int year) { this.model = model; this.year = year; } correctly uses parameters and assigns them to fields with this.
  3. Final Answer:

    public Car(String model, int year) { this.model = model; this.year = year; } -> Option B
  4. Quick Check:

    Constructor syntax = no return type + class name [OK]
Hint: Constructor name = class name, no return type [OK]
Common Mistakes:
  • Adding return type to constructor
  • Not using 'this' to assign fields
  • Using void or other return types
3.

What will be the output of the following Java code?

class Book {
    String title;
    int pages;

    Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    void display() {
        System.out.println(title + ": " + pages + " pages");
    }
}

public class Main {
    public static void main(String[] args) {
        Book b = new Book("Java Basics", 250);
        b.display();
    }
}
medium
A. Java Basics: 250 pages
B. Java Basics pages
C. 250 pages
D. Compilation error

Solution

  1. Step 1: Understand constructor initialization

    The constructor sets title to "Java Basics" and pages to 250.
  2. Step 2: Analyze display method output

    The display() method prints the title, colon, pages, and "pages" text.
  3. Final Answer:

    Java Basics: 250 pages -> Option A
  4. Quick Check:

    Constructor sets fields, display prints them [OK]
Hint: Constructor sets fields, display prints combined string [OK]
Common Mistakes:
  • Ignoring 'this' keyword effect
  • Expecting only title or pages printed
  • Assuming syntax error without reason
4.

Identify the error in this Java class with a parameterized constructor:

public class Student {
    String name;
    int age;

    public Student(String n, int a) {
        name = n;
        age = a;
    }

    public Student() {
        name = "Unknown";
        age = 0;
    }

    public void Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
medium
A. No default constructor defined
B. Missing return type in parameterized constructor
C. The method named Student with void return type is not a constructor
D. Fields name and age are not declared

Solution

  1. Step 1: Check constructor definitions

    Constructors have no return type and match class name exactly.
  2. Step 2: Identify method named Student with void

    The method public void Student(String name, int age) is not a constructor but a method, which is confusing.
  3. Final Answer:

    The method named Student with void return type is not a constructor -> Option C
  4. Quick Check:

    Constructors have no return type [OK]
Hint: Constructors never have a return type, not even void [OK]
Common Mistakes:
  • Writing constructors with void return type
  • Confusing methods with constructors
  • Missing default constructor when needed
5.

Given the class below, what will be the output when creating two Employee objects with different parameters and printing their details?

class Employee {
    String name;
    double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public void printInfo() {
        System.out.println(name + " earns $" + salary);
    }
}

public class Test {
    public static void main(String[] args) {
        Employee e1 = new Employee("Alice", 50000);
        Employee e2 = new Employee("Bob", 60000);
        e1.printInfo();
        e2.printInfo();
    }
}
hard
A. Runtime error due to uninitialized fields
B. Alice earns $60000.0 Bob earns $50000.0
C. Compilation error due to missing default constructor
D. Alice earns $50000.0 Bob earns $60000.0

Solution

  1. Step 1: Analyze object creation with parameters

    Two Employee objects are created with names and salaries passed to the parameterized constructor.
  2. Step 2: Check printInfo output

    Each object's printInfo() prints the name and salary correctly.
  3. Final Answer:

    Alice earns $50000.0 Bob earns $60000.0 -> Option D
  4. Quick Check:

    Parameterized constructor sets fields, printInfo shows them [OK]
Hint: Each object holds its own values set by constructor [OK]
Common Mistakes:
  • Mixing up values between objects
  • Expecting errors without reason
  • Assuming default constructor is needed here