1. What is the main purpose of a loop in C++?
Correct Answer: a) It repeats statements while a condition is true
Explanation:
Loops allow a block of statements to be executed repeatedly based on a condition or counter.
2. Which is the correct syntax of a for loop in C++?
Correct Answer: b) for (int i = 0; i < 5; ++i) { }
Explanation:
A C++ for loop uses initialization, condition, and update expressions separated by semicolons inside parentheses.
3. Which is the correct syntax of a while loop in C++?
Correct Answer: c) while (i < 5) { }
Explanation:
A while loop places its condition in parentheses, followed by the loop body.
4. Which is the correct syntax of a do-while loop in C++?
Correct Answer: d) do { } while (condition);
Explanation:
A do-while loop ends with a semicolon after the condition.
5. In a for loop, when is the initialization expression executed?
Correct Answer: a) Once before the loop starts
Explanation:
The initialization part of a for loop usually runs only once, before the first iteration.
6. When is the condition of a while loop checked?
Correct Answer: b) Before each iteration
Explanation:
A while loop tests its condition before executing the loop body.
7. In a for loop, when is the update expression usually executed?
Correct Answer: c) After each iteration
Explanation:
The update expression is executed after the loop body and before the next condition check.
8. A loop repeats as long as its condition is what?
Correct Answer: d) True
Explanation:
Loop execution continues while the loop condition evaluates to true.
9. Can the body of a loop be empty in C++?
Correct Answer: a) Yes
Explanation:
A loop can have an empty statement as its body, often by placing a semicolon after the loop header.
10. Which statement creates an infinite while loop?
Correct Answer: b) while (true)
Explanation:
The condition true never becomes false, so the loop continues indefinitely unless exited.
11. How many main expressions are separated by semicolons in a standard for loop header?
Correct Answer: c) Three
Explanation:
A standard for loop header contains initialization, condition, and update expressions.
12. What happens if a while loop condition is false initially?
Correct Answer: d) The body is not executed
Explanation:
A while loop checks the condition first, so a false initial condition means zero iterations.
13. Which of the following is allowed in a for loop header?
Correct Answer: a) Leaving the initialization part empty
Explanation:
A for loop may omit initialization, condition, or update, but the semicolons remain.
14. In for (int i = 0; i < 5; ), which part is missing?
Correct Answer: b) Update expression
Explanation:
The third part after the second semicolon is the update expression, and it is empty here.
15. Which of these creates a valid infinite loop?
Correct Answer: c) while (true)
Explanation:
while (true) repeats forever unless broken by break, return, or program termination.
16. Which for loop creates an infinite loop?
Correct Answer: d) for (;;)
Explanation:
When all three parts are omitted, the missing condition is treated as true.
17. What does break do inside a loop?
Correct Answer: a) It exits the loop immediately
Explanation:
break terminates the nearest enclosing loop or switch statement.
18. What does continue do inside a loop?
Correct Answer: b) It skips the remaining body and goes to the next iteration
Explanation:
continue ends the current iteration early and proceeds to the next loop cycle.
19. What is the scope of a variable declared in a for loop initialization in standard C++?
Correct Answer: c) Limited to the for statement
Explanation:
A variable declared in the for initialization is not accessible after the loop ends.
20. Which statement is true about a while loop?
Correct Answer: d) It may execute zero times
Explanation:
If the while condition is false initially, the loop body is never executed.
21. What is printed by for (int i = 0; i < 5; ++i) cout << i;?
Correct Answer: a) 01234
Explanation:
The loop prints i from 0 through 4 and stops before i becomes 5.
22. Which of the following is invalid C++ syntax?
Correct Answer: b) while (int i = 0; i < 3; ++i) { }
Explanation:
A while loop condition cannot contain semicolon-separated initialization, condition, and update parts like a for loop.
23. Which loop always executes its body at least once?
Correct Answer: c) do-while
Explanation:
A do-while loop checks the condition after executing the body.
24. Which is the correct syntax of a range-based for loop?
Correct Answer: d) for (auto x : vec) { }
Explanation:
A range-based for loop uses a colon between the loop variable and the container or range.
25. What kind of expression can be used as the condition of a while loop?
Correct Answer: a) Any expression contextually convertible to bool
Explanation:
C++ allows many scalar expressions in loop conditions by converting them to Boolean values.
26. What happens if the condition is omitted in a for loop header?
Correct Answer: b) It is treated as true
Explanation:
An omitted for-loop condition is considered true, which can create an infinite loop.
27. Which separator is used between multiple expressions in the update part of a for loop?
Correct Answer: c) Comma
Explanation:
The comma operator can separate multiple update expressions in a for loop.
28. What should be used to execute multiple statements in a while loop body?
Correct Answer: d) Braces
Explanation:
Braces create a compound statement so that multiple statements are controlled by the loop.
29. What is printed by for (int i = 0; i < 3; ++i) cout << i;?
Correct Answer: a) 012
Explanation:
The loop prints 0, then 1, then 2, and stops when i becomes 3.
30. What is printed by while (false) cout << 1;?
Correct Answer: b) No output
Explanation:
The condition is false, so the loop body never executes.
31. What is printed by for (; false;) cout << 1;?
Correct Answer: c) No output
Explanation:
The condition is explicitly false, so the loop body is skipped.
32. What is true about while (true) if there is no break or exit mechanism?
Correct Answer: d) It runs indefinitely
Explanation:
A true condition keeps the loop running forever unless something exits it.
33. How many iterations does for (int i = 0; i < 0; ++i) execute?
Correct Answer: a) Zero
Explanation:
The condition i < 0 is false initially, so the body never runs.
34. Which operator is commonly used in loop conditions?
Correct Answer: b) <
Explanation:
Relational operators such as < are often used to control loop repetition.
35. In a for loop, when is the condition evaluated?
Correct Answer: c) Before each iteration
Explanation:
The for loop checks its condition before executing the loop body for each iteration.
36. Where is the counter usually updated in a while loop?
Correct Answer: d) Inside the loop body
Explanation:
A while loop has no built-in update expression, so the loop variable is usually changed inside the body.
37. Which is a common off-by-one error?
Correct Answer: a) Using < instead of <= when an inclusive range is needed
Explanation:
Choosing the wrong relational operator can cause one too many or one too few iterations.
38. What is printed by for (int i = 0; i <= 5; ++i) cout << i;?
Correct Answer: b) 012345
Explanation:
The condition i <= 5 allows the loop to print 5 as the final value.
39. What is printed by for (int i = 1; i < 5; ++i) cout << i;?
Correct Answer: c) 1234
Explanation:
The loop starts at 1 and stops before i becomes 5.
40. Which while loop correctly counts down from 3 to 1 if i is initially 3?
Correct Answer: d) while (i > 0) --i;
Explanation:
The loop continues while i is positive and decreases i each iteration.
41. What does break do in nested loops?
Correct Answer: a) It exits the innermost loop
Explanation:
break applies to the nearest enclosing loop or switch statement.
42. What does continue do in a while loop?
Correct Answer: b) It jumps to the condition check
Explanation:
In a while loop, continue skips the remaining body and re-evaluates the condition.
43. What does continue do in a for loop?
Correct Answer: c) It skips the body and then performs the update expression
Explanation:
In a for loop, continue transfers control to the update expression before the next condition check.
44. Which code is most likely to cause an infinite loop?
Correct Answer: d) while (i > 0) { }
Explanation:
If i is positive and never changed, the condition remains true forever.
45. In for (int i = 0; i < 5; ++i), when is i initialized?
Correct Answer: a) Before the loop starts
Explanation:
The initialization expression runs once at the beginning of the for loop.
46. Which statement is true about while (x = f())?
Correct Answer: b) It is legal if the assigned value contextually converts to bool
Explanation:
Assignment can be used in a condition, though it is often confused with equality comparison.
47. Which operator separates multiple update expressions such as i++, j-- in a for loop?
Correct Answer: c) Comma
Explanation:
The comma operator allows multiple expressions in the update part of a for loop.
48. What is the body of while (x < 5);?
Correct Answer: d) An empty statement
Explanation:
The semicolon immediately after the condition makes the loop body empty.
49. Can the loop variable be modified inside the body of a for loop?
Correct Answer: a) Yes
Explanation:
The loop variable is an ordinary variable and can be modified inside the loop body.
50. Which symbols must surround the condition in a while loop?
Correct Answer: b) Parentheses
Explanation:
C++ requires the while condition to be enclosed in parentheses.
51. When is the condition of a do-while loop evaluated?
Correct Answer: c) After the body
Explanation:
A do-while loop executes the body first, then tests the condition.
52. What is for (;;) equivalent to?
Correct Answer: d) while (true)
Explanation:
An omitted condition in a for loop is treated as true.
53. If int x is nonzero, what is true about while (x)?
Correct Answer: a) The condition is true
Explanation:
Nonzero values convert to true in a Boolean context.
54. What is a range-based for loop mainly used for?
Correct Answer: b) Iterating over elements in a container or range
Explanation:
Range-based for simplifies traversal of arrays, vectors, and other ranges.
55. Which for loop header is valid?
Correct Answer: c) for (;;)
Explanation:
A valid for loop header must contain two semicolons, even if all three expressions are empty.
56. Which direction can a while loop count?
Correct Answer: d) Upward or downward
Explanation:
A while loop can increment or decrement its controlling variable.
57. What is printed by for (int i = 1; i <= 3; i++) cout << i;?
Correct Answer: a) 123
Explanation:
The loop prints 1, 2, and 3, then stops when i becomes 4.
58. What is printed by int x = 0; while (x < 3) { cout << x; ++x; }?
Correct Answer: b) 012
Explanation:
The loop prints x before incrementing it, so it prints 0, 1, and 2.
59. What is printed by for (int i = 5; i > 0; --i) cout << i;?
Correct Answer: c) 54321
Explanation:
The loop prints 5 through 1 and stops when i becomes 0.
60. What is printed by int x = 3; while (x > 0) { --x; cout << x; }?
Correct Answer: d) 210
Explanation:
The loop decrements x before printing, so it prints 2, 1, and 0.
61. What is a nested loop?
Correct Answer: a) A loop inside another loop
Explanation:
Nested loops allow repeated execution of inner code for each outer loop iteration.
62. If an outer loop runs N times and an inner loop runs M times, how many times does the inner body usually run?
Correct Answer: b) N times M total
Explanation:
Each outer iteration causes a full execution of the inner loop.
63. What is printed by for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) cout << i << j;?
Correct Answer: c) 00011011
Explanation:
The loops print i followed by j for pairs 0 0, 0 1, 1 0, and 1 1.
64. Which for initialization correctly declares two int variables?
Correct Answer: d) for (int i = 0, j = 0; i < 3; ++i)
Explanation:
Multiple variables of the same type can be declared in the initialization part using commas.
65. Which statement is true about the initialization part of a for loop?
Correct Answer: a) It can declare a loop variable
Explanation:
The initialization part often declares and initializes the loop control variable.
66. Which statement is true about the body of a while loop?
Correct Answer: b) It can be a single statement or a block
Explanation:
A while loop controls one statement, which may be a compound statement enclosed in braces.
67. Which statement is true about the update expression in a for loop?
Correct Answer: c) It can be empty
Explanation:
The update expression may be omitted, leaving only the semicolon in place.
68. What can a while loop condition call?
Correct Answer: d) A function
Explanation:
The condition can be any expression, including a function call whose result is converted to bool.
69. Can a for loop condition call a function?
Correct Answer: a) Yes
Explanation:
A for loop condition can be any valid expression, including a function call.
70. To avoid an infinite loop, what should usually happen to the loop variable?
Correct Answer: b) It should be modified toward termination
Explanation:
The loop condition must eventually become false for the loop to stop.
71. What is true about for (unsigned i = 5; i >= 0; --i)?
Correct Answer: c) It can become infinite because unsigned wraparound keeps the condition true
Explanation:
An unsigned integer is never negative, so i >= 0 remains true even after decrementing from zero.
72. Assuming i is initially less than 3 and is not changed, which loop is infinite?
Correct Answer: d) while (i < 3) { continue; }
Explanation:
continue skips to the condition, but i never changes, so the condition remains true.
73. What happens if you try to use a variable declared only in a for initialization statement after the loop in standard C++?
Correct Answer: a) It causes a compile error
Explanation:
The variable declared in the for initialization is not visible after the loop ends.
74. If a counter variable is declared before a while loop, what is true after the loop ends?
Correct Answer: b) The variable is still accessible
Explanation:
A variable declared outside the loop remains in scope after the loop.
75. If break appears in the body of a for loop before the update expression, what happens?
Correct Answer: c) The loop exits immediately
Explanation:
break terminates the loop before the update expression is executed.
76. If break appears in the body of a while loop, what happens?
Correct Answer: d) The loop exits immediately
Explanation:
break exits the nearest enclosing loop immediately.
77. In a for loop, what normally happens after continue?
Correct Answer: a) The update expression executes
Explanation:
continue causes control to jump to the for loop update expression.
78. In a while loop, what happens after continue?
Correct Answer: b) The condition is re-evaluated
Explanation:
continue skips the rest of the body and returns to the while condition.
79. In a do-while loop, what does continue cause?
Correct Answer: c) A jump to the condition check
Explanation:
continue skips the remaining body and evaluates the do-while condition.
80. Can a for loop iterate zero times?
Correct Answer: d) Yes, if the condition is false initially
Explanation:
A for loop checks its condition before the first iteration.
81. Can a while loop iterate zero times?
Correct Answer: a) Yes, if the condition is false initially
Explanation:
A while loop is a pre-test loop, so it may execute zero times.
82. How many times does a do-while loop execute its body at minimum?
Correct Answer: b) One
Explanation:
The body runs before the condition is checked.
83. How many iterations does for (int i = 10; i < 5; ++i) execute?
Correct Answer: c) Zero
Explanation:
The condition is false initially because 10 is not less than 5.
84. Which statement can be used to exit a while (true) loop?
Correct Answer: d) break
Explanation:
break terminates the loop even if the condition is true.
85. What is one valid use of a for loop with an empty body?
Correct Answer: a) It can perform work in the condition or update
Explanation:
Sometimes all necessary loop work is done in the header, leaving the body empty.
86. What is while (x < 10);?
Correct Answer: b) A loop with an empty body
Explanation:
The semicolon after the condition acts as the loop body.
87. What does a semicolon immediately after a for loop header do?
Correct Answer: c) It makes the loop body empty
Explanation:
The semicolon is an empty statement controlled by the loop.
88. How many times does while (false); execute?
Correct Answer: d) Zero
Explanation:
The condition is false before the first iteration.
89. What is printed by int i; for (i = 0; i < 3; i++); cout << i;?
Correct Answer: a) 3
Explanation:
The semicolon makes the loop body empty, so i becomes 3 and then cout prints it once.
90. What happens with int x = 0; while (x < 3); { x++; }?
Correct Answer: b) It creates an infinite loop
Explanation:
The semicolon makes the while body empty, so x is not changed inside the loop.
91. In a simple for loop, what is usually true about using i++ instead of ++i in the update expression?
Correct Answer: c) They produce the same result
Explanation:
When the incremented value is not used in the same expression, postfix and prefix increment behave the same for loop control.
92. What is printed by int x = 0; while (x++ < 3) cout << x;?
Correct Answer: d) 123
Explanation:
Postfix increment increments x after the comparison, so the body prints 1, 2, and 3.
93. What is printed by int x = 0; for (int i = 0; i < 3; ++i) { x += i; } cout << x;?
Correct Answer: a) 3
Explanation:
The loop adds 0, 1, and 2 to x, giving a total of 3.
94. What is printed by int x = 0; while (++x < 3) cout << x;?
Correct Answer: b) 12
Explanation:
Prefix increment changes x before the comparison, so the loop prints 1 and 2.
95. What is true about modifying the for loop variable inside the loop body?
Correct Answer: c) It is allowed but can affect loop behavior
Explanation:
Changing the loop variable inside the body can alter the number of iterations or cause infinite loops.
96. Which condition is valid in a while loop?
Correct Answer: d) An expression such as x != 0
Explanation:
while accepts any expression that can be converted to bool.
97. Why can for (int i = 0; i != 5; i += 2) be dangerous?
Correct Answer: a) It may skip the exact value and become infinite
Explanation:
If i jumps from 4 to 6, it never equals 5, so the condition remains true.
98. A while loop using a sentinel value is most useful when what determines termination?
Correct Answer: b) Input or runtime data
Explanation:
Sentinel-controlled loops continue until a special value or condition appears.
99. Which loop is often preferred when the number of iterations is known in advance?
Correct Answer: c) for
Explanation:
A for loop conveniently combines initialization, condition, and update in one header.
100. Which loop is often preferred when termination depends on a condition rather than a count?
Correct Answer: d) while
Explanation:
A while loop is well suited for repeating until a condition becomes false.









