What values are produced by Python's range(2, 11, 2)?
05 Loops and Repetition Online Quiz Questions
Use this free practice quiz with 20 questions to review 05 Loops and Repetition, test your knowledge, and prepare for your next test or exam.
True or false: A for loop is usually a clear choice when a program should perform an action once for every item in a collection.
- A
True
- B
False
Complete the sentence: In the loop that counts values in [4, 7, 10, 13, 16], even_count is increased only when a number is .
What is the programming term for a variable that records how many times an event occurs?
What does a break statement do when it executes inside a nested loop?
- A
It terminates every loop in the program.
- B
It skips the current iteration and continues the same loop.
- C
It immediately terminates the innermost loop.
- D
It restarts the outer loop from its first iteration.
True or false: In Python, a loop's else clause runs when the loop finishes normally, but not when the loop ends through break.
- A
True
- B
False
Select all situations for which a Python for loop is generally the clearest choice.
- A
Processing every item in a list
- B
Repeating a known number of times
- C
Continuing until user input changes a condition
- D
Iterating through a range of values
Complete the debugging advice: To make while count <= 5 terminate in the example, the loop must update the .
A nested loop has an outer loop that runs 3 times and an inner loop that runs 4 times for each outer iteration. How many times does the inner loop body execute in total?
Which initialization correctly prepares an accumulator named product to multiply the factors 2, 3, and 4?
- A
product = 0
- B
product = 1
- C
product = -1
- D
product = None
Select all practices that help prevent an input-driven while loop from becoming infinite.
- A
The loop condition can eventually become false
- B
The state controlling the condition is updated toward termination
- C
The loop must always execute exactly five times
- D
Unexpected input is handled so it cannot prevent progress
True or false: The continue statement skips the remainder of the current iteration and begins the next iteration.
- A
True
- B
False
Describe an algorithm that uses a loop to find the largest value in [12, 5, 19, 8, 14]. State the initialization, the comparison and update performed for each remaining value, and the final result.
A loop classifies the values [3, -2, 0, 8, -1] into positive, negative, and zero categories. Which final counter values are correct?
- A
positive = 2, negative = 3, zero = 1
- B
positive = 3, negative = 2, zero = 1
- C
positive = 3, negative = 1, zero = 2
- D
positive = 2, negative = 2, zero = 1
A program must print each name in a list exactly once, and the list itself determines how many names there are. Which approach best matches the material?
- A
Use a
forloop to process each item in the collection. - B
Use a
whileloop with a condition that is always true. - C
Use
breakbefore examining any items. - D
Use
range()without referring to the collection.
Which sequence is produced by range(2, 11, 2) in a Python for loop?
- A
2, 4, 6, 8, 10, 12
- B
1, 3, 5, 7, 9, 11
- C
2, 4, 6, 8, 10
- D
2, 3, 4, 5, 6, 7, 8, 9, 10
Consider numbers = [4, 7, 10, 13, 16] and a counter that starts at zero and increases only when number % 2 == 0. What is the counter's final value?
- A
5
- B
3
- C
2
- D
0
In a nested loop, the statement break executes inside the inner loop. What does it do?
- A
Both loops stop immediately.
- B
Only the outer loop stops.
- C
The
breakis ignored inside nested loops. - D
Only the inner loop stops.
What programming term names the step that establishes a loop's starting value or data source?
A nested loop has for row in range(3): as its outer loop and for column in range(4): as its inner loop, with one statement in the inner body. How many times does that body statement execute?