Suppose count is initialized to 10. How many times does the body of while (count < 5) { ... } execute, assuming no code outside the shown loop changes count?
04 Loops and Iteration Online Quiz Questions
Use this free practice quiz with 20 questions to review 04 Loops and Iteration, test your knowledge, and prepare for your next test or exam.
A C++ do-while loop executes its body at least once, even when its condition is initially false.
- A
True
- B
False
Which C++ loop statement is most appropriate for processing every integer from 0 through n - 1 when n is known before the loop begins?
A while loop tests its condition executing its body.
Which statement correctly describes the execution of a C++ for loop?
- A
The update runs before the first condition check.
- B
The condition is checked only after the body runs.
- C
The update expression runs after each completed iteration.
- D
The initialization runs again after every iteration.
If a break statement runs inside an inner loop nested within an outer loop, it exits only the inner loop.
- A
True
- B
False
Select all statements that correctly describe what happens when continue executes in a C++ for loop.
- A
It skips the remaining statements in the current iteration.
- B
It runs the
forloop's update expression before the next condition check. - C
It immediately terminates the entire loop.
- D
It allows the loop condition to be checked again.
A loop initializes total to 0 and executes total += number for every integer number from 1 through 10, inclusive. What is the final value of total?
In sentinel-controlled input, the special value that signals the end of input is called a .
What does this loop print?
for (int number = 1; number <= 10; ++number) { if (number % 2 == 0) { continue; } std::cout << number << ' ';}
- A
1 2 3 4 5 6 7 8 9 10 - B
1 3 5 7 9 - C
2 4 6 8 10 - D
No values are printed.
Select all recommendations that the material gives for writing reliable and maintainable loops.
- A
Initialize loop-control variables before using them.
- B
Ensure each iteration makes progress toward termination.
- C
Always use
breakinstead of a loop condition. - D
Use braces even for a one-statement body.
Explain the differences among C++ while, do-while, and for loops. For each type, describe when its condition is tested, whether its body can execute zero times, and a situation in which that loop is an appropriate choice. Also explain why initialization and progress toward termination matter.
How many times does the inner body execute in this code?
for (int row = 1; row <= 3; ++row) { for (int column = 1; column <= 4; ++column) { std::cout << '(' << row << ',' << column << ") "; }}
- A
4
- B
7
- C
12
- D
16
What does this C++ code print?
int count = 2;
while (count < 5) {
std::cout << count << ' ';
++count;
}
- A
2 3 4 5
- B
2 3 4
- C
1 2 3 4
- D
3 4 5
A variable choice is initialized to 0 and then used in this loop:
do {
std::cout << "Menu";
} while (choice != 0);
How many times does the body execute?
- A
The body executes zero times
- B
The body executes until
choicebecomes 1 - C
The body executes exactly once
- D
The program does not compile
What does this C++ code print?
for (int number = 1; number <= 5; ++number) {
if (number % 2 == 0) {
continue;
}
std::cout << number << ' ';
}
- A
1 3 5
- B
1 2 3 4 5
- C
2 4
- D
1 3 5 6
How many lines does this nested-loop program output?
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 3; ++column) {
if (column == 1) {
break;
}
std::cout << row << ", " << column << '\n';
}
}
- A
1 line
- B
2 lines
- C
6 lines
- D
3 lines
True or false: In a while loop, placing continue before the update of the variable used in the loop condition can cause an infinite loop.
- A
True
- B
False
What integer value is stored in total after this loop finishes?
int total = 0;
for (int value = 1; value <= 5; ++value) {
total += value;
}
What programming term names a variable that stores a running total or combined result while a loop executes?
Related study packs and tools
2 Digital Logic
A 14-question medium-difficulty quiz on Boolean algebra, logic gates, truth tables, and combinational circuits.
03 — Python Programming Fundamentals
A focused set of flashcards covering Python variables, data types, expressions, control flow, functions, input, output, and common programming practices.
04 Conditionals and Boolean Logic
Learn how Boolean expressions, logical operators, and conditional structures let programs make reliable decisions, including how to handle boundaries, branch order, and testing.
6 Computer Memory
Learn how computer memory stores and addresses information, how RAM and ROM differ, and how caches and the memory hierarchy balance speed, capacity, and cost.
07 Trees and Binary Search Trees
A progressive guide to tree structure, binary-tree traversals, binary search tree operations, deletion, balancing, and the way tree height determines time and space costs.
12 — Foundations Capstone: Integrating Computer Science
A practical guide to integrating problem definition, data representation, modular programming, algorithms, complexity analysis, testing, correctness, and ethical evaluation into a responsible computer science capstone.
Interrupts
Reviews how interrupts support event handling and multitasking, and how Linux system calls use the RISC-V `ecall` instruction and register conventions.
OS Processes
This quiz covers process management, context switching and scheduling, multitasking, traps and interrupts, and RISC-V privilege transitions.
Systemcalls
A quiz on operating-system system calls, including privileged-operation requests, process creation and execution with fork, exec, and waitpid, and signal handling.