ProgrammingEasyWipro · Cognizant technical pattern

A Java class declares only the constructor Account(String id) and no other constructor. What happens with the statement new Account()?

  1. A. The compiler adds a default no-argument constructor automatically
  2. B. It compiles, and id is set to null
  3. C. It throws a NullPointerException at run time
  4. D. Compilation error: no no-argument constructor exists

Correct answer: D. Compilation error: no no-argument constructor exists

The compiler supplies a default constructor only when a class declares NO constructors. Once Account(String) exists, new Account() does not compile unless a no-arg constructor is written.

More OOP with C++ and Java questions

Exams that ask this topic