If a class has only a parameterized constructor and you attempt to instantiate with no arguments, what happens?

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

If a class has only a parameterized constructor and you attempt to instantiate with no arguments, what happens?

Explanation:
When a class provides only a parameterized constructor, no default no-argument constructor is created automatically. That means trying to instantiate the class with no arguments has no matching constructor, so the compiler flags an error. The runtime won’t fill in default values or add a constructor at runtime. If you want to create objects without arguments, you need to explicitly define a no-argument constructor (or use a form of constructor overloading or default parameters where the language supports it). This is why a compilation error occurs in this scenario.

When a class provides only a parameterized constructor, no default no-argument constructor is created automatically. That means trying to instantiate the class with no arguments has no matching constructor, so the compiler flags an error. The runtime won’t fill in default values or add a constructor at runtime. If you want to create objects without arguments, you need to explicitly define a no-argument constructor (or use a form of constructor overloading or default parameters where the language supports it). This is why a compilation error occurs in this scenario.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy