ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
int a = 5;
int &r = a;
r = 10;
int b = 20;
r = b;
b = 30;
cout << a << " " << r;
  1. A. 30 30
  2. B. 20 30
  3. C. 10 30
  4. D. 20 20

Correct answer: D. 20 20

A reference is bound once, at initialisation, and cannot be re-seated. r = b copies the value 20 into a; it does not make r refer to b. So later b = 30 has no effect: a and r are both 20.

More OOP with C++ and Java questions

Exams that ask this topic