Difficulty: Medium
Correct Answer: Newline followed by 0 0 1 (printed contiguously as 001)
Explanation:
Introduction / Context:
This question illustrates the behaviour of static arrays, default initialization, and the post increment operator used in an assignment. It requires careful evaluation of a[i] = i++ and understanding of the resulting values.
Given Data / Assumptions:
Concept / Approach:
In C, the expression i++ evaluates to the current value of i, then increments i as a side effect. Since i is used as an array index, we must use its value before and after the increment carefully. Because a is static, any element that is not explicitly assigned remains zero. We need to compute the values of a[0], a[1], and i after the assignment.
Step-by-Step Solution:
Step 1: Initially, i is 0 and all elements of a are 0.Step 2: Evaluate a[i] = i++;. The index expression i uses the current value 0, so we are assigning to a[0].Step 3: The right-hand side i++ evaluates to 0 as a value, and then i is incremented to 1.Step 4: After this statement, a[0] is set to 0, i is now 1, and a[1] is still 0 because it has never been assigned.Step 5: The printf call prints a newline, then a[0], a[1], and i. Thus it prints 0, 0, and 1 with no spaces, resulting in 001 after the newline.
Verification / Alternative check:
You can rewrite the core logic as temp = i; a[i] = temp; i = i + 1; to see that a[0] receives 0 and i becomes 1. Running such a program in a compiler produces the expected output.
Why Other Options Are Wrong:
Option B would require a[0] to be 1, which is not the case.Option C suggests a[1] becomes 1, but a[1] is never modified and remains 0.Option D would require both a[0] and a[1] to be changed to 1, which does not happen.
Common Pitfalls:
Programmers sometimes misread post increment expressions and think the value used on the right side is the incremented value. Remember that i++ yields the old value, then increments i after evaluation.
Final Answer:
The program prints Newline followed by 0 0 1 (printed contiguously as 001).
Discussion & Comments