Python Control Flow: Loops Questions and Answers — Questions and Answers
Question 1: What is the final value of `total` after this `while` loop finishes?
- 15
- 10 (Correct answer)
- 5
- 4
Correct answer: 10
The loop executes for `count` values of 0, 1, 2, 3, and 4. The `total` accumulates the sum of these numbers: 0 + 1 + 2 + 3 + 4 = 10. When `count` becomes 5, the condition `5 < 5` is false, and the loop terminates.
Question 2: In Python, under which circumstance is the `else` block of a `for` loop executed?
- It is always executed after the loop finishes.
- It is only executed if the loop is terminated by a `break` statement.
- It is only executed if the loop completes all its iterations without being terminated by a `break` statement. (Correct answer)
- It is only executed if the iterable is empty and the loop body never runs.
Correct answer: It is only executed if the loop completes all its iterations without being terminated by a `break` statement.
The `else` clause for a loop runs only when the loop has completed its iterations normally. If the loop is exited prematurely with a `break` statement, the `else` block is skipped. It is a way to run code when a `break` was not encountered.
Question 3: A developer wants to sum only the odd numbers from a list. What will the following code print?
- 15
- 6
- 9 (Correct answer)
- 1
Correct answer: 9
The `continue` statement skips the remainder of the current iteration and proceeds to the next. When `num` is even (2 and 4), the `continue` statement is executed, and the line `total += num` is skipped. Therefore, only the odd numbers (1, 3, 5) are added to `total`, resulting in 1 + 3 + 5 = 9.
Question 4: What is the final value of `count` after the following nested loops complete execution?
- 5
- 3
- 2
- 6 (Correct answer)
Correct answer: 6
This is a nested loop. The outer loop runs 3 times (for i = 0, 1, 2). For each iteration of the outer loop, the inner loop runs completely, which is 2 times (for j = 0, 1). Therefore, the innermost statement `count += 1` is executed a total of 3 * 2 = 6 times.
Question 5: What will be the output of the following Python code snippet that uses a `break` statement?
- 10 (Correct answer)
- 15
- 45
- 5
Correct answer: 10
The `for` loop iterates through the numbers generated by `range(10)`. The `break` statement causes the immediate termination of the loop when `i` becomes 5. Consequently, the loop only processes the values of `i` from 0 up to 4. The sum accumulated in `result` is 0 + 1 + 2 + 3 + 4, which equals 10.
Question 6: Which of the following `while` loops will result in an infinite loop?
- i = 5 while i > 0: i -= 1
- i = 0 while i < 5: i = 1 (Correct answer)
- i = 1 while i < 10: i *= 2
- i = 0 while i < 5: i += 1
Correct answer: i = 0 while i < 5: i = 1
An infinite loop occurs when the loop's condition never becomes false. In the correct option, `i` is initialized to 0 and the condition `i < 5` is true. Inside the loop, `i` is repeatedly set to 1. Since the value of `i` never changes in a way that would make the condition false (i.e., it never becomes 5 or greater), the loop will run forever.
What is the final value of `total` after this `while` loop finishes?