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

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

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