0
0
Javaprogramming~20 mins

Parameterized constructor in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.