Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? int x = 5; int y = x++ + 2; cout << x << ” ” << y;

Correct answer A. 6 7

Explanation

The postfix x++ yields the old value of x (5) and then increments x to 6. So y receives 5 + 2 = 7 while x ends at 6. A prefix ++x would have produced 8 for y. Here x is modified only once in the full expression, so the behaviour is well defined.

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.