ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
struct W { W() { cout << "W"; } };
int main() {
    W w1;
    W w2();
    W w3{};
    cout << "!";
}
  1. A. WW!
  2. B. WWW!
  3. C. W!
  4. D. Compile-time error at W w2();

Correct answer: A. WW!

W w2(); is the 'most vexing parse': it declares a function named w2 returning W, so no object is created. w1 and w3 (brace value-initialisation) both call the constructor.

More OOP with C++ and Java questions

Exams that ask this topic