Free PCEP Controlling loop execution Questions and Answers 1 — Questions and Answers
Question 1: What will be the output of the following code snippet?
```python
for i in range(5):
if i == 3:
break
print(i, end=' ')
```
- 0 1 2 3 4
- 0 1 2 (Correct answer)
- 0 1 2 4
- 3
Correct answer: 0 1 2
The loop iterates through numbers from 0 to 4. The `print` statement executes for `i` values 0, 1, and 2. When `i` becomes 3, the `if` condition is met, and the `break` statement is executed, which terminates the loop immediately.
Question 2: What is the output of this code?
```python
for i in range(5):
if i == 3:
continue
print(i, end=' ')
```
- 0 1 2
- 0 1 2 3 4
- 0 1 2 4 (Correct answer)
- 4
Correct answer: 0 1 2 4
The loop iterates from 0 to 4. When `i` is 3, the `if` condition is true, and the `continue` statement is executed. This statement skips the rest of the current iteration (the `print` statement) and proceeds to the next iteration (when `i` is 4).
Question 3: What does the following code print?
```python
i = 0
while i < 5:
print(i, end=' ')
i += 1
if i == 3:
break
```
- 0 1 2 3
- 0 1 2 3 4
- 0 1 2 (Correct answer)
- 0 1
Correct answer: 0 1 2
The `while` loop starts with `i = 0`. It prints 0, then increments `i` to 1. It prints 1, increments `i` to 2. It prints 2, increments `i` to 3. Now the condition `i == 3` is true, and the `break` statement terminates the loop.
Question 4: What is the output of this `while` loop?
```python
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i, end=' ')
```
- 1 2 3 4 5
- 1 2 4 5 (Correct answer)
- 0 1 2 4
- 0 1 2 3 4
Correct answer: 1 2 4 5
Inside the loop, `i` is incremented first. When `i` becomes 3, the `continue` statement is executed, skipping the `print(i)` for that iteration. The loop continues, printing the values 1, 2, 4, and 5.
Question 5: What is the output of the following nested loop?
```python
for i in range(2):
for j in range(2):
if i == j:
break
print((i, j), end=' ')
```
- (1, 0) (Correct answer)
- (0, 1) (1, 0)
- (0, 1)
- No output
Correct answer: (1, 0)
The `break` statement only affects the innermost loop it is in. When `i` is 0, the inner loop runs for `j=0`. The condition `i == j` is true, so `break` is executed, and nothing is printed. When `i` is 1, the inner loop runs for `j=0`. The condition is false, so `(1, 0)` is printed. Then `j` becomes 1, the condition `i == j` is true, and the inner loop breaks.
Question 6: What will be printed by this code?
```python
for num in range(4):
if num == 2:
break
else:
print("Done")
print(num)
```
- Done
2
- 2 (Correct answer)
- Done
3
- 3
Correct answer: 2
The `for` loop's `else` block is executed only if the loop completes its iterations without being terminated by a `break` statement. In this case, the loop breaks when `num` is 2. Therefore, the `else` block is skipped. The final `print(num)` statement prints the last value of `num` before the loop was exited, which is 2.
Question 7: What is the output of the following code?
```python
for i in range(2):
print(f"Outer {i}")
for j in range(2):
if j == 1:
continue
print(f" Inner {j}")
```
- Outer 0
Inner 0
Outer 1
Inner 0 (Correct answer)
- Outer 0
Inner 0
Inner 1
Outer 1
Inner 0
Inner 1
- Outer 0
Inner 0
- Outer 0
Outer 1
Correct answer: Outer 0
Inner 0
Outer 1
Inner 0
The `continue` statement only skips the current iteration of the inner loop. For each outer loop iteration (`i=0` and `i=1`), the inner loop starts. When `j` is 0, it prints. When `j` is 1, the `continue` skips the print statement and the inner loop finishes, then the outer loop proceeds to its next iteration.