C Loops - Tricky MCQ

Previous C Loops (while, do-while, for) Next

Tricky Questions on Loops

1

What is the key difference between while and do-while loops?

Correct Answer: D) Both A and B

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.

2

Which loop is guaranteed to execute at least once?

Correct Answer: C) do-while loop

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.

3

What is wrong with: for(;;);

Correct Answer: D) Nothing - it's an infinite loop

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.

4

Which loop is best when number of iterations is unknown?

Correct Answer: B) while loop

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.

5

What is the output: int i=0; while(i++ < 5); printf("%d", i);

Correct Answer: C) 6

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).

6

Which statement about break in loops is FALSE?

Correct Answer: C) break transfers control to loop condition

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.

7

What is the output: for(int i=0; i<3; i++) { if(i==1) continue; printf("%d", i); }

Correct Answer: B) 02

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.

8

Which is NOT equivalent to while(1)?

Correct Answer: C) while(true)

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.

9

What is wrong with: for(int i=0; i<10; i++); { printf("%d", i); }

Correct Answer: C) Variable i scope error

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.

10

Which loop variable declaration is valid in C89?

Correct Answer: B) int i; for(i=0; i<10; i++)

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.

11

What is "loop unrolling" optimization?

Correct Answer: A) Replacing loop with repeated code

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.

12

What is "loop invariant" in optimization?

Correct Answer: A) Expression with same value each iteration

Loop invariant is expression whose value doesn't change across iterations. Compilers can optimize by moving invariant calculations outside loop (code motion). Improves performance by computing once instead of each iteration.

13

What is "off-by-one error" in loops?

Correct Answer: D) Both A and B

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.

14

What is "loop fusion" optimization?

Correct Answer: A) Combining adjacent loops into one

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.

15

What is "zero-trip loop" scenario?

Correct Answer: D) Both A and C

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.

Previous C Loops (while, do-while, for) Next