Multiple choice

#include<stdio.h> #include<conio.h> main() { int a[3],i; for(i=0;i<3;i++) a[i]=i++; for(i=0;i<3;i++) printf(" %d",a[i]); }

  1. 0 Garbage Value 2

  2. 0 1 2

  3. 1 2 3

  4. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The expression i++ uses post-increment, meaning the current value of i is assigned to a[i], then i is incremented. First iteration: a[0]=0, i becomes 1. Second: a[1]=1, i becomes 2. Third: a[2]=2, i becomes 3. But wait - after i=2, we assign a[2]=2, then i becomes 3, so loop exits. However, when i=0, a[0]=0, then i becomes 1. When i=1, a[1]=1, then i becomes 2. When i=2, a[2]=2, then i becomes 3. Actually, the issue is that the loop condition is checked BEFORE the increment in the for statement. Let me reconsider: for(i=0; i<3; i++) - the increment happens AFTER each iteration. So: i=0, assign a[0]=0, then i++ makes i=1; i=1, assign a[1]=1, then i++ makes i=2; i=2, assign a[2]=2, then i++ makes i=3. But there's undefined behavior here because i is modified twice in the same expression (a[i]=i++). In practice, the assignment happens before the increment, so a[0]=0, a[1]=1, a[2]=2. However, option A says '0 Garbage Value 2' which suggests something else is happening. The key is that after i becomes 3, the loop exits, but a[3] is never set. However, the array only has 3 elements (indices 0,1,2). The answer '0 Garbage Value 2' suggests that a[1] contains garbage. This is due to the undefined behavior of i++ where the assignment and increment order is not guaranteed. The most likely outcome is indeed that a[1] ends up with garbage because of the sequence point violation.