ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
struct M { operator int() const { return 7; } };
int main() { M m; cout << m + 1; }
  1. A. 8
  2. B. 71
  3. C. Compile-time error: no operator+ for M
  4. D. 7

Correct answer: A. 8

A (non-explicit) conversion operator lets M be converted to int implicitly, so m + 1 becomes 7 + 1.

More OOP with C++ and Java questions

Exams that ask this topic