Recall & Review
beginner
What is constructor overloading in Java?
Constructor overloading means having multiple constructors in a class with the same name but different parameters. It allows creating objects in different ways.Click to reveal answer
beginner
Why do we use constructor overloading?
We use constructor overloading to create objects with different initial values or setups easily, without writing multiple classes.
Click to reveal answer
intermediate
How does Java decide which constructor to call when overloading is used?
Java looks at the number and types of arguments passed when creating an object and matches them to the constructor with the corresponding parameter list.
Click to reveal answer
intermediate
Can constructors have the same number of parameters but different types in overloading?
Yes, constructors can have the same number of parameters but different types, and Java will choose the correct one based on argument types.
Click to reveal answer
beginner
Show a simple example of constructor overloading in Java.
class Box {
int width, height;
Box() { width = 0; height = 0; }
Box(int w, int h) { width = w; height = h; }
}
Here, Box has two constructors: one with no parameters and one with two parameters.Click to reveal answer
What does constructor overloading allow you to do?
✗ Incorrect
Constructor overloading means having multiple constructors with different parameter lists in the same class.
Which of these is a valid constructor overloading example?
✗ Incorrect
Constructors must have different parameter lists. Option A shows two constructors with different parameters.
If a class has constructors: A(), A(int), A(String), which constructor is called by new A("hello")?
✗ Incorrect
The constructor with a String parameter matches the argument "hello".
Can constructor overloading have two constructors with the same parameter types but different return types?
✗ Incorrect
Constructors do not have return types, so overloading depends on parameter lists only.
What happens if you define no constructor in a Java class?
✗ Incorrect
Java automatically provides a default no-argument constructor if none is defined.
Explain constructor overloading and why it is useful in Java.
Think about how you can create objects in different ways.
You got /3 concepts.
Describe how Java chooses which constructor to call when multiple constructors exist.
Focus on how Java matches arguments to constructors.
You got /3 concepts.