ProgrammingModerateProduct-company placement · C++

What happens when this is compiled?

What happens when this is compiled?
struct A { A(int) { } };
int main() { A a; }
  1. A. Compile-time error: A has no default constructor
  2. B. It compiles; a is zero-initialised
  3. C. It compiles; A(int) is called with 0
  4. D. It compiles with a warning only

Correct answer: A. Compile-time error: A has no default constructor

The compiler generates a default constructor only if the class declares no constructors at all. Declaring A(int) suppresses it; add A() = default; to get it back.

More OOP with C++ and Java questions

Exams that ask this topic