Difficulty: Medium
Correct Answer: 2 1 3 1 4 1 5 1 6 1 7 0
Explanation:
Introduction / Context:
This problem tests comprehension of for-loop structure where the “increment” expression performs the printing, and the loop’s body updates the boolean control variable y based on a post-increment of x.
Given Data / Assumptions:
Concept / Approach:
The statement y = x++ <= 5; evaluates the comparison using the old value of x, then increments x. The printing occurs after this assignment in each iteration. Thus, the printed x is one greater than the value used in the comparison.
Step-by-Step Solution:
Iter 1: body sets y = (1 <= 5) → 1; x becomes 2. Print “2 1”.Iter 2: y = (2 <= 5) → 1; x=3. Print “3 1”.Iter 3: y = (3 <= 5) → 1; x=4. Print “4 1”.Iter 4: y = (4 <= 5) → 1; x=5. Print “5 1”.Iter 5: y = (5 <= 5) → 1; x=6. Print “6 1”.Iter 6: y = (6 <= 5) → 0; x=7. Print “7 0”.Now y is 0 → loop terminates.
Verification / Alternative check:
Move the printf into the body before or after the assignment to see how sequencing changes the displayed values; the given order matches the post-expression behavior.
Why Other Options Are Wrong:
Shorter sequences stop prematurely; options without the final “7 0” ignore the last iteration where y flips to 0.
Common Pitfalls:
Forgetting that the post-increment applies after comparison; misplacing when the printf executes in a for-loop; assuming printf happens before the assignment.
Final Answer:
2 1 3 1 4 1 5 1 6 1 7 0
Discussion & Comments