Assuming standard headers and using namespace std, what does this C++ code print? int a = 10; int& r = a; r = 20; int b = r; b++; cout << a << ” ” << b;
Correct answer C. 20 21
Explanation
A reference is an alias, so assigning r = 20 changes a itself to 20. The statement int b = r copies the current value into a separate variable b, so incrementing b to 21 does not affect a. Only names declared as references share storage; ordinary variables initialised from a reference get their own copy.
Answer checked How we write and check questions
Discussion