Which statement best explains why a while loop can execute its body zero times?
05. Iteration and Loops Quiz Online Quiz Questions
Use this free practice quiz with 20 questions to review 05. Iteration and Loops, test your knowledge, and prepare for your next test or exam.
True or false: In Python-like syntax, range(1, 5) includes the value 5.
- A
True
- B
False
What is the name for a special input value that signals the end of a sentinel-controlled loop?
In the three-part loop design of initialization, condition, and , which part changes the state so the loop can eventually finish?
A program examines [4, 7, 2, 9, 6] and increases a variable only when the current value is even. What is the variable functioning as?
- A
It stores the sum of every number in the list.
- B
It stores the largest number seen so far.
- C
It increases when a specified event is observed.
- D
It ensures that the loop condition is always true.
True or false: A break statement inside the inner loop of a nested-loop structure automatically terminates both the inner and outer loops.
- A
True
- B
False
How many times does the innermost body execute?\n\nfor row in range(2):\n for column in range(3):\n process()
In a loop, the statement skips the remainder of the current iteration and moves to the next one.
A program must apply the same operation to every item in a list whose contents are already available. Which design is most appropriate?
- A
Use a for loop to visit every item in a known list.
- B
Use a while loop and manually track an index even though the list is the direct source of repetition.
- C
Use a sentinel-controlled while loop for every list traversal.
- D
Use an infinite loop because the list length is known.
Design and explain a loop that counts the positive values in [-2, 0, 4, 7, -1]. State the initialization, the condition that causes the counter to increase, the final result, and an invariant that remains true after each iteration.
A program needs to create a list of even values from an existing list. Which approach is safest?
- A
Remove each matching item from the list during the same traversal.
- B
Build a new collection while traversing the original list.
- C
Insert new items at the current position during every iteration.
- D
Change the list length whenever the loop condition is checked.
What values does this loop print?\n\nfor n in range(2, 6):\n print(n)
- A
2, 3, 4, 5, 6
- B
2, 3, 4, 5
- C
1, 2, 3, 4, 5
- D
3, 4, 5, 6
What is the result of this code?\n\ni = 0\nwhile i < 0:\n print(i)\n i = i + 1
- A
It prints 0 once.
- B
It prints 0 repeatedly.
- C
It prints nothing.
- D
It raises an error because the condition is false.
A sentinel-controlled loop reads the values 4, 7, and then -1, where -1 is the sentinel. What total should the loop produce if it adds each ordinary value and excludes the sentinel?
- A
11
- B
10
- C
12
- D
-1
Which initialization is appropriate for total before a loop computes the sum of all values in [3, 5, 2]?
- A
total = 0
- B
total = 1
- C
total = 3
- D
total = 10
True or false: In a nested loop, a break statement inside the inner loop automatically terminates both the inner and outer loops.
- A
True
- B
False
What integer does this code print?\n\nvalues = [-2, 0, 4, 7, -1]\npositive_count = 0\n\nfor value in values:\n if value > 0:\n positive_count += 1\n\nprint(positive_count)
How many numbers does this loop print?\n\nfor number in range(1, 6):\n if number == 3:\n continue\n print(number)
A command-processing loop should print every command except "quit", then stop when "quit" is entered. Which two redesigns preserve that behavior while providing a reliable path to termination? Select all correct choices.
- A
Use a condition such as
while command != "quit"and read the next command at the end of each non-quit iteration. - B
Keep an explicit
breakfor the quit command and read the next command after processing each non-quit command. - C
Add a
breakimmediately after processing the first non-quit command. - D
Set
commandto"quit"before entering the loop so that the loop has a guaranteed exit. - E
Remove every update to
commandbecause the loop condition is checked automatically.
You are testing a loop that finds the largest value in a list. Which test cases are especially useful for exposing boundary or initialization problems? Select all correct choices.
- A
An empty list, to reveal whether the algorithm handles zero items safely.
- B
A one-item list, to test the smallest nonempty input.
- C
A list whose largest value is the first item.
- D
A list whose largest value is the last item.
- E
Only one typical multi-item list with the largest value somewhere in the middle.