What values are produced by this loop?
for number in range(2, 6):
print(number)
Use this free practice quiz with 20 questions to review 4 Loops and Iteration, test your knowledge, and prepare for your next test or exam.
What values are produced by this loop?
for number in range(2, 6):
print(number)
2, 3, 4, 5, 6
2, 3, 4, 5
1, 2, 3, 4, 5
3, 4, 5, 6
In a nested loop, a break statement is inside the inner loop. What does the break do?
Both loops stop immediately.
Only the outer loop stops.
Only the inner loop stops.
The break statement is ignored in nested loops.
A program repeatedly asks for input until the user enters a special stopping value, but the number of inputs is unknown. Which loop is the best fit?
Use a for loop because every input sequence has a fixed length.
Use a for loop because it automatically validates the input.
Use a while loop because the sentinel determines when repetition ends.
Use a nested loop because the input may contain multiple words.
Select all statements that correctly describe Python's loop else clause.
It runs when a for loop exhausts its iterable without executing break.
It runs when a while loop's condition becomes false without executing break.
It always runs whenever the loop body contains an if statement.
It is useful for reporting that a search found no match when no break occurred.
Select all recommended approaches when a program needs to remove or filter items from a collection while processing it.
Build a separate collection containing the desired results.
Modify the same collection directly during traversal because the loop automatically adjusts.
Iterate over a copy when changes to the original collection are necessary.
Use a filtering loop to construct a new collection.
In Python, continue skips the remainder of the current iteration and starts the next iteration of the loop.
True
False
In for number in range(3):, assigning number = 100 inside the loop changes the next value that range(3) supplies.
True
False
What term names a special value that signals the end of input in a sentinel-controlled loop?
A counter starts at 0 and increases by 1 for each score in the list scores = [72, 91, 64, 88, 95] that is at least 70. What is the counter's final value?
In range(start, stop, step), the value is excluded from the generated sequence.
A loop can repeatedly request input until the user provides an acceptable value.
Explain when a programmer should choose a for loop instead of a while loop in Python. Include how the controlling state affects termination.
Given products = ["pen", "notebook", "eraser"] and prices = [1.50, 3.25, 0.75], what does zip(products, prices) do when used in a loop?
It pairs every product with every price.
It pairs each product with the price at the same position.
It sorts both lists before pairing them.
It repeats the last price until all products have been processed.
In a nested loop, the inner loop completes all of its iterations for each one iteration of the outer loop.
True
False
Which loop is the most direct choice for printing every item in the list colors = ["red", "green", "blue"]?
Use a while loop because lists cannot be used with for loops.
Use a for loop because it processes items supplied by an iterable.
Use a while loop because for loops are only for numeric calculations.
Use a for loop because it always repeats forever.
What is the term for a special value that signals the end of input in a repetition pattern?
Which expression is the clearest way to obtain both the index and item while iterating through animals = ["cat", "dog", "bird"]?
zip(animals)
enumerate(animals)
range(animals)
len(animals)
Which approach is safest when filtering items from a collection during iteration?
Construct a new collection containing the desired items.
Change the list length during every iteration.
Assign a new value to the loop variable and keep modifying the list.
Use a nested loop that removes items from the original list.
What does this Python code print?
values = [4, 7, 2, 9]
total = 0
for value in values:
if value % 2 == 0:
total += value
else:
total -= 1
print(total)
4
6
21
22
What integer does this Python program print?
total = 1
value = 2
while value <= 8:
total *= value
value *= 2
print(total)