C Loops - Tricky MCQ
Tricky Questions on Loops
Basic Level (15 Questions)
What is the key difference between while and do-while loops?
While loop checks condition first, then executes body if true. Do-while executes body first, then checks condition, guaranteeing at least one execution. Both can be infinite with appropriate conditions.
Which loop is guaranteed to execute at least once?
Only do-while guarantees at least one execution because it tests condition after loop body. For and while loops check condition first, so they may not execute if condition is initially false.
What is wrong with: for(;;);
This is a valid infinite for loop. All three parts are optional. Missing condition defaults to true, creating infinite loop. A semicolon after for() means empty loop body.
Which loop is best when number of iterations is unknown?
While loops are ideal when iterations depend on dynamic condition not known beforehand. For loops excel when iterations are predetermined. Do-while for when at least one execution is needed regardless of condition.
What is the output: int i=0; while(i++ < 5); printf("%d", i);
Post-increment i++ returns original value then increments. Loop executes for i=0,1,2,3,4. When i=5, condition fails but i becomes 6 after increment. Empty loop body (semicolon after while).
Which statement about break in loops is FALSE?
Break exits the entire loop immediately, transferring control to the statement after the loop, not back to condition. It works in for, while, do-while, and switch. In nested loops, break exits only the innermost loop.
What is the output: for(int i=0; i<3; i++) { if(i==1) continue; printf("%d", i); }
Continue skips remaining loop body and proceeds to next iteration. When i=1, continue executes, skipping printf. Loop prints i=0, skips i=1, prints i=2. Output is "02" without spaces.
Which is NOT equivalent to while(1)?
While(true) requires including stdbool.h or defining true as 1. In standard C without bool support, true is not defined. While(1), for(;;), and do{ }while(1) are all valid infinite loops.
What is wrong with: for(int i=0; i<10; i++); { printf("%d", i); }
Variable i declared inside for loop (C99) has scope limited to the loop. Semicolon after for() creates empty loop body. The block { printf(...); } is outside loop scope, so i is undeclared there.
Which loop variable declaration is valid in C89?
Declaring loop variable inside for() is C99 feature. C89 requires variables declared before loop. Many compilers accept C99 syntax by default but strict C89 mode rejects it.
What is "loop unrolling" optimization?
Loop unrolling reduces loop overhead by replicating loop body multiple times, decreasing iteration count. Improves performance by reducing branch prediction misses and enabling instruction-level parallelism. Done manually or by compiler optimization.
What is "loop invariant" in optimization?
What is "off-by-one error" in loops?
Off-by-one error occurs when loop boundary condition is wrong by one. Common with <= instead of < or vice versa. Causes incorrect iteration count, often leading to array bounds violation or missing/extra elements.
What is "loop fusion" optimization?
Loop fusion merges consecutive loops with same iteration space into single loop. Reduces loop overhead and improves cache locality. Only possible when loops don't have data dependencies that would change results.
What is "zero-trip loop" scenario?
Zero-trip loop has condition false initially, so body never executes. While and for loops support zero iterations. Do-while always executes at least once, so cannot be zero-trip. Important for handling edge cases like empty arrays.
Hard Level (15 Questions)
`for (i=0; i<10; i++);` followed by `printf("%d", i);` — i is declared outside. Value?
Loop exits when i<10 fails at i==10; the final value of i is 10.
Infinite loop: `while(1) { }` — ISO C says:
If the loop has no observable behavior, compilers may assume it terminates or remove it under as-if rules; practical systems still hang.
`for (int i=0; i<3; i++) { int i=5; }` — inner i shadows. After loop, outer i in C99 block?
for-init declares i in the loop scope; inner block declares different i; no outer i exists unless declared outside.
`continue` in a switch inside a while loop:
continue applies to the innermost enclosing loop, not switch.
Which loop always executes its body at least once?
do-while tests condition after the first execution.
`for (;;);` is equivalent to:
Empty condition in for means true forever, like while(1).
Nested loops: `for(i=0;i<3;i++) for(j=0;j<i;j++)` total inner iterations?
When i=0 inner never runs; i=1 runs once; i=2 runs twice: total 3.
`break` only exits:
break exits one enclosing loop or switch level only.
Loop variable `i` modified in body: `for(i=0;i<10;i++) i+=2;` iterations count?
i+=2 in body plus i++ in for step advances i by 3 each iteration; fewer than 5 full passes to reach 10.
VLA in loop: `for (int n=2; n<5; n++) { int a[n]; }` — lifetime of `a`?
VLA lifetime is the block of each iteration; storage may be reused each time.
`while (scanf("%d", &x)==1)` stops when:
scanf returns EOF on end/error, 0 on mismatch, positive on successful assignments.
Which is NOT equivalent generally? `for(a;b;c) d;` vs:
Placing a only once before while matches for-init once; putting a inside while repeats init wrongly in option C as stated.
`goto` out of nested loops to single label:
goto can leave loops; automatic variables in skipped scopes were not initialized if jump bypassed init.
Empty body `while(*s++);` advances s:
Post-increment reads then advances; loop continues until '\0' processed, s ends past terminator.
`for (i=1; i<100; i<<=1)` — effect on i?
i<<=1 is compound assignment left shift; doubles i until condition fails or UB on overflow.