ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
int main() {
    int a = 1, b = 2;
    int& r = a;
    r = b;
    r = 5;
    cout << a << b;
}
  1. A. 15
  2. B. 25
  3. C. 52
  4. D. 12

Correct answer: C. 52

A reference cannot be reseated: r = b assigns 2 to a, and r = 5 then assigns 5 to a. b is never changed.

More OOP with C++ and Java questions

Exams that ask this topic