ProgrammingModerateMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct N {
    int v;
    N& operator++()   { v += 10; return *this; }
    N  operator++(int) { N t = *this; v += 1; return t; }
};
int main() {
    N a{0};
    N b = a++;
    N c = ++a;
    cout << a.v << " " << b.v << " " << c.v;
}
  1. A. 11 1 11
  2. B. 11 0 11
  3. C. 11 0 10
  4. D. 20 0 11

Correct answer: B. 11 0 11

a++ calls the int overload: it returns the old value (b = 0) and adds 1 (a = 1). ++a adds 10, making a = 11, and c is copied from the returned reference, so c = 11.

More OOP with C++ and Java questions

Exams that ask this topic