Difficulty: Easy
Correct Answer: The program will print the output 10 21.
Explanation:
Introduction / Context:
This problem tests C++ operator precedence with pointers, the difference between postfix ++ on a pointer versus on a referenced variable, and how these affect the underlying integers when using the classic iostream.h style program.
Given Data / Assumptions:
Concept / Approach:
In the expression *ptr++, postfix ++ has higher precedence than *. Therefore *ptr++ is parsed as *(ptr++). The pointer is advanced to the next int, and the dereferenced value (from the old address) is evaluated and discarded. No write occurs to x, so x remains 10. By contrast, ref++ increments the aliased object (y) by 1, so y becomes 21.
Step-by-Step Solution:
Verification / Alternative check:
Rewriting as *(ptr++); ref++; clarifies that the first statement has no side-effect on x. If the intent were to increment x, one would write (*ptr)++.
Why Other Options Are Wrong:
Common Pitfalls:
Misreading *ptr++ as (*ptr)++; forgetting postfix ++ binds tighter than *; assuming pointer increment changes the pointee.
Final Answer:
The program will print the output 10 21.
Discussion & Comments