Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.