Difficulty: Medium
Correct Answer: The program will print output 2 6.
Explanation:
Introduction / Context:
This tests how references alias variables and how post-increment (x++) yields the old value while still performing a side effect. Assignments may overwrite the incremented value, which is a classic source of confusion in interview questions.
Given Data / Assumptions:
Concept / Approach:
Track each statement carefully: compute the rvalue (old value for post-increment), perform the side effect (increment), then perform the assignment to the left-hand side which may overwrite the incremented value.
Step-by-Step Solution:
Verification / Alternative check:
Replace references with direct variables (m in place of x, n in place of y); the trace is identical because x and y alias m and n.
Why Other Options Are Wrong:
They assume the incremented values survive the subsequent overwriting assignments; they do not in this sequence.
Common Pitfalls:
Forgetting that post-increment returns the old value and that later assignments through references can revert the increment.
Final Answer:
The program will print output 2 6.
Discussion & Comments