C++ Loops (For, While)

1 min read

1. What is the main purpose of a loop in C++?

a) It repeats statements while a condition is true
b) It executes a statement only if the condition is false
c) It defines a new variable
d) It terminates the program
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++?

a) for i = 0 to 5 { }
b) for (int i = 0; i < 5; ++i) { }
c) for (i < 5; ++i) int i = 0;
d) for int i = 0; i < 5; ++i { }
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++?

a) while i < 5 { }
b) while i < 5 do { }
c) while (i < 5) { }
d) while {i < 5}
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++?

a) do { } while condition
b) do { } while (condition)
c) do while (condition) { }
d) do { } while (condition);
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?

a) Once before the loop starts
b) Before every iteration
c) After the loop ends
d) Never
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?

a) After each iteration
b) Before each iteration
c) Only once before the program ends
d) Never
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?

a) Before the first iteration
b) Only when the condition is false
c) After each iteration
d) Never
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?

a) False
b) Equal to zero
c) Initialized
d) True
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++?

a) Yes
b) No
c) Only in while loops
d) Only in do-while loops
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?

a) while (false)
b) while (true)
c) while (0)
d) while (i < 0)
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?

a) One
b) Two
c) Three
d) Four
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?

a) The body executes once
b) The loop becomes infinite
c) A compile error occurs
d) The body is not executed
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?

a) Leaving the initialization part empty
b) Removing both semicolons
c) Removing the parentheses
d) Replacing semicolons with commas
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?

a) Initialization
b) Update expression
c) Opening parenthesis
d) Condition
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?

a) while (false)
b) for (int i = 0; i < 1; ++i)
c) while (true)
d) do { } while (false);
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?

a) for (int i = 0; i < 1; ++i)
b) for (; false; )
c) for (int i = 0; i < 1; )
d) for (;;)
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?

a) It exits the loop immediately
b) It restarts the loop from the beginning
c) It skips the update expression
d) It pauses the program
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?

a) It exits the loop
b) It skips the remaining body and goes to the next iteration
c) It stops the program
d) It repeats the current body without checking the condition
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++?

a) Global scope
b) Whole program
c) Limited to the for statement
d) No scope
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?

a) It always runs at least once
b) It cannot contain break
c) It must have an else clause
d) It may execute zero times
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;?

a) 01234
b) 12345
c) 012345
d) 5
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?

a) while (false) { }
b) while (int i = 0; i < 3; ++i) { }
c) for (;;) { }
d) do { } while (false);
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?

a) for
b) while
c) do-while
d) None of the above
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?

a) for (x in vec)
b) for each x in vec
c) for (auto x -> vec)
d) for (auto x : vec) { }
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?

a) Any expression contextually convertible to bool
b) Only an integer literal
c) Only a character literal
d) Only a bool variable
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?

a) It is treated as false
b) It is treated as true
c) A compile error occurs
d) The loop executes zero times
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?

a) Semicolon
b) Colon
c) Comma
d) Dot
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?

a) A semicolon
b) A comma
c) Parentheses
d) Braces
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;?

a) 012
b) 123
c) 0123
d) 3
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;?

a) 1
b) No output
c) Compile error
d) Infinite loop
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;?

a) 1
b) Compile error
c) No output
d) Infinite loop
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?

a) It exits immediately
b) It executes once
c) It causes a compile error
d) It runs indefinitely
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?

a) Zero
b) One
c) Infinite
d) Compile error
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?

a) +
b) <
c) =
d) &
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?

a) After the update expression
b) Only once
c) Before each iteration
d) Never
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?

a) In the while keyword
b) In the condition only
c) Outside the program
d) Inside the loop body
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?

a) Using < instead of <= when an inclusive range is needed
b) Using braces around the loop body
c) Using break correctly
d) Using continue correctly
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;?

a) 01234
b) 012345
c) 12345
d) 123456
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;?

a) 12345
b) 01234
c) 1234
d) 2345
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?

a) while (i < 3) ++i;
b) while (i == 3);
c) while (i > 3) --i;
d) while (i > 0) --i;
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?

a) It exits the innermost loop
b) It exits all loops immediately
c) It exits the outer loop only
d) It does nothing
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?

a) It exits the loop
b) It jumps to the condition check
c) It stops the program
d) It restarts initialization
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?

a) It skips the update expression
b) It exits the loop
c) It skips the body and then performs the update expression
d) It stops the program
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?

a) while (i < 3) { ++i; }
b) for (int i = 0; i < 3; ++i) { }
c) while (false) { }
d) while (i > 0) { }
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?

a) Before the loop starts
b) After the loop ends
c) Only if the condition is true
d) Never
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())?

a) It is always a syntax error
b) It is legal if the assigned value contextually converts to bool
c) Assignment is never allowed in a condition
d) It always creates an infinite loop
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?

a) Semicolon
b) Colon
c) Comma
d) Space
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);?

a) x++
b) A braced block
c) A compile error
d) An empty statement
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?

a) Yes
b) No
c) Only in the update expression
d) Only in the condition
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?

a) Braces
b) Parentheses
c) Square brackets
d) Angle brackets
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?

a) Before the body
b) Never
c) After the body
d) Only once before compilation
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?

a) while (false)
b) do { } while (false);
c) switch (0)
d) while (true)
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)?

a) The condition is true
b) The condition is false
c) A compile error occurs
d) The loop always runs exactly once
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?

a) Defining functions
b) Iterating over elements in a container or range
c) Allocating memory
d) Terminating the program
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?

a) for (;)
b) for ()
c) for (;;)
d) for :
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?

a) Only upward
b) Only downward
c) Only once
d) Upward or downward
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;?

a) 123
b) 012
c) 1234
d) 321
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; }?

a) 0123
b) 012
c) 123
d) 12
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;?

a) 543210
b) 4321
c) 54321
d) 01234
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; }?

a) 321
b) 3210
c) 21
d) 210
Correct Answer: d) 210
Explanation:
The loop decrements x before printing, so it prints 2, 1, and 0.

61. What is a nested loop?

a) A loop inside another loop
b) An if statement inside a loop
c) A loop inside a function
d) A function inside a 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?

a) N times total
b) N times M total
c) M minus N times
d) N plus M times
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;?

a) 0123
b) 0011
c) 00011011
d) 0101
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?

a) for (int i = 0, int j = 0; i < 3; ++i)
b) for (int i = 0; int j = 0; i < 3; ++i)
c) for (i, j; i < 3; ++i)
d) for (int i = 0, j = 0; i < 3; ++i)
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?

a) It can declare a loop variable
b) It is required in every for loop
c) It cannot be empty
d) It appears after the condition
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?

a) It must always use braces
b) It can be a single statement or a block
c) It cannot contain another loop
d) It executes only once
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?

a) It is required
b) It runs before the condition
c) It can be empty
d) It cannot modify variables
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?

a) Only constants
b) Only literals
c) Only main
d) A function
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?

a) Yes
b) No
c) Only if the function is main
d) Only once in the entire program
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?

a) It should remain unchanged
b) It should be modified toward termination
c) It should be global
d) It should be constant
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)?

a) It runs exactly six times
b) It stops after reaching zero
c) It can become infinite because unsigned wraparound keeps the condition true
d) It causes a compile error
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?

a) while (i < 3) { ++i; }
b) while (false) { continue; }
c) for (int i = 0; i < 3; ++i) { continue; }
d) while (i < 3) { continue; }
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++?

a) It causes a compile error
b) It uses its last value
c) It becomes global
d) It remains in scope
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?

a) The variable is no longer accessible
b) The variable is still accessible
c) The variable becomes uninitialized
d) The variable is automatically deleted
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?

a) The update expression still executes
b) The loop continues normally
c) The loop exits immediately
d) A compile error occurs
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?

a) The condition is checked again
b) The body repeats
c) The program ends
d) The loop exits immediately
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?

a) The update expression executes
b) The loop exits
c) The initialization repeats
d) The program ends
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?

a) The loop ends
b) The condition is re-evaluated
c) The initialization runs again
d) break is triggered
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?

a) Program termination
b) The condition to be ignored
c) A jump to the condition check
d) The initialization to repeat
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?

a) Never
b) Only with do-while
c) Only if the update expression is missing
d) Yes, if the condition is false initially
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?

a) Yes, if the condition is false initially
b) No
c) Only with break
d) Only if the body is empty
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?

a) Zero
b) One
c) Two
d) Unlimited
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?

a) Five
b) Ten
c) Zero
d) Infinite
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?

a) continue
b) A semicolon
c) A comment
d) break
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?

a) It can perform work in the condition or update
b) It defines a function
c) It replaces all if statements
d) It disables compilation
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);?

a) A syntax error
b) A loop with an empty body
c) Always an infinite loop
d) Not a loop
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?

a) It creates a braced body
b) It causes a compile error
c) It makes the loop body empty
d) It exits the loop
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?

a) Once
b) Infinitely
c) Twice
d) Zero
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;?

a) 3
b) 012
c) 0123
d) Infinite loop
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++; }?

a) It increments x to 3
b) It creates an infinite loop
c) It prints 0
d) It causes a compile error
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?

a) They always produce different results
b) They cause syntax errors
c) They produce the same result
d) They are not allowed
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;?

a) 012
b) 1234
c) 12
d) 123
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;?

a) 3
b) 6
c) 012
d) Compile error
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;?

a) 123
b) 12
c) 012
d) 23
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?

a) It is forbidden
b) It always causes a compile error
c) It is allowed but can affect loop behavior
d) It is required
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?

a) Only constants
b) Only literals
c) Only Boolean variables
d) An expression such as x != 0
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?

a) It may skip the exact value and become infinite
b) It is always a syntax error
c) The != operator cannot be used in for loops
d) The += operator cannot be used in update expressions
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?

a) A fixed number of iterations
b) Input or runtime data
c) No condition
d) A required single execution
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?

a) do-while
b) goto
c) for
d) switch
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?

a) for always
b) do-while always
c) switch
d) while
Correct Answer: d) while
Explanation:
A while loop is well suited for repeating until a condition becomes false.
← Previous: C++ Inheritance and Polymorphism - MCQs
Next →: C++ Memory Management MCQs
New
No Image

C++ Control Flow / If-Else MCQs Conditional Statements

1. What is the main purpose of a loop in C++? a) It repeats statements while a condition is true…

By MCQs Generator
New
No Image

C++ Exception / Error Handling - MCQs

1. What is the main purpose of a loop in C++? a) It repeats statements while a condition is true…

By MCQs Generator
NewC++ Variables & Data Types MCQs

C++ Variables & Data Types MCQs

Variables and data types form the core building blocks of any C++ application, defining how data is stored, represented, and…

By MCQs Generator