Challenge - 5 Problems
Parameterized Constructor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Look at how the constructor assigns values to the instance variables.
β Incorrect
The parameterized constructor sets the model and year for each Car object. The display method prints these values. So car1 prints 'Toyota 2020' and car2 prints 'Honda 2018'.
β Predict Output
intermediate1: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); } }
Attempts:
2 left
π‘ Hint
The constructor sets the price variable directly.
β Incorrect
The parameterized constructor assigns the value 29.99 to the instance variable price. So printing b.price outputs 29.99.
π§ Debug
advanced2: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(); } }
Attempts:
2 left
π‘ Hint
Check how the constructor assigns values to instance variables.
β Incorrect
The constructor parameters shadow the instance variables. The assignments 'name = name;' and 'age = age;' assign the parameters to themselves, not to the instance variables. So instance variables remain default null and 0.
π Syntax
advanced2:00remaining
Which option causes a compilation error in parameterized constructor?
Which of the following constructor definitions will cause a compilation error?
Attempts:
2 left
π‘ Hint
Check for missing semicolons and correct syntax inside the constructor.
β Incorrect
Option B is missing a semicolon after 'this.age = age', causing a compilation error.
π Application
expert3: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); } }
Attempts:
2 left
π‘ Hint
Remember that e3 is a reference to e1, not a new object.
β Incorrect
Only two Employee objects are created by the constructor calls. e3 points to the same object as e1. Changing e3.name changes e1.name. So output shows 'Mike 101' and 'Jane 102'.