Controlling Loop Execution in Python: Mastering Break and Continue
PCEP Certification Practice Test - Questions, Answers and Explanations
Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic of "controlling loop execution with break and continue." The questions use various formats including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: What happens when a break statement is encountered inside a loop?
- It skips to the next iteration of the loop.
- It exits the loop immediately.
- It raises a SyntaxError.
- It restarts the loop from the beginning.
Answer: B) It exits the loop immediately
Explanation: The break statement terminates the loop immediately, regardless of the loop's condition.
Question 2: Which of the following statements are true about the continue statement? (Choose all that apply) (Choose all that apply)
- It exits the loop entirely.
- It skips the rest of the current loop iteration and moves to the next iteration.
- It can be used in both for and while loops.
- It stops the execution of the entire program.
Answer: B) It skips the rest of the current loop iteration and moves to the next iteration.
C) It can be used in both for and while loops.
Explanation: The continue statement skips the rest of the code in the current iteration and moves to the next iteration of the loop. It works in both for and while loops.
Question 3: Complete the following code to exit the loop when the variable num is equal to 5.
for num in range(10): if num == 5: ______ print(num)▼
Answer: break
Explanation: The break statement is used to exit the loop when num equals 5. The loop stops immediately when break is encountered.
Question 4: What will be the output of the following code?
for i in range(5): if i == 2: continue print(i)
- 0 1 2 3 4
- 0 1 3 4
- 0 1 2
- 2 3 4
Answer: B) 0 1 3 4
Explanation: The continue statement skips the iteration when i == 2, so 2 is not printed. The loop then continues with i == 3 and i == 4.
Question 5: Insert the appropriate statement to skip the current iteration when x is odd.
for x in range(6): if x % 2 != 0: ______ print(x)▼
Answer: continue
Explanation: The continue statement skips the rest of the code in the loop when x is odd, so only even numbers are printed.
Question 6: What does the following code do?
i = 0 while i < 5: if i == 3: break print(i) i += 1
- Prints 0 1 2 3 4
- Prints 0 1 2
- Prints 0 1 2 3
- No output
Answer:B) Prints 0 1 2
Explanation: The loop breaks when i == 3, so only 0 1 2 are printed before the loop exits.
Question 7: Which of the following loops will print only even numbers between 1 and 10? (Choose all that apply)
for i in range(1, 11): if i % 2 != 0: continue print(i)
for i in range(1, 11): if i % 2 == 0: print(i) else: continue
for i in range(1, 11): if i % 2 == 0: continue print(i)
for i in range(1, 11): if i % 2 != 0: break print(i)
Answer: A) and B)
Explanation: Both A and B use continue to skip printing odd numbers, so only even numbers are printed. In option C, odd numbers are printed, and in option D, the loop breaks at the first odd number.
Question 8: Arrange the steps to create a loop that prints numbers from 0 to 9 but skips the number 5.
- Increment the loop variable.
- Print the loop variable.
- Check if the loop variable equals 5.
- Use continue to skip the number 5.
Answer: 3, 4, 2, 1
Explanation: First, check if the loop variable equals 5. If it does, use continue to skip that iteration. Otherwise, print the loop variable and then increment it.
Question 9: Complete the following code to ensure the loop prints numbers until it encounters 4, at which point it stops.
for i in range(10): if i == 4: ______ print(i)▼
Answer: break
Explanation: The break statement stops the loop when i equals 4, so the loop does not print any numbers greater than 3.
Question 10: What will be the output of the following code?
for i in range(5): if i == 3: break print(i) else: print("Done")
- 0 1 2 Done
- 0 1 2 3
- 0 1 2
- Done
Answer: C) 0 1 2
Explanation: The loop breaks when i == 3, so "Done" is not printed because the else block is skipped when break is used.
Question 11: Insert the correct statement to exit the loop when the variable i is a multiple of 7.
for i in range(1, 20): if i % 7 == 0: ______ print(i)▼
Answer: break
Explanation: The break statement exits the loop when i is a multiple of 7, so only numbers before the first multiple of 7 are printed.
Question 12: What happens if a continue statement is used in a while loop?
- The loop terminates immediately.
- The loop starts from the beginning with the same value of the loop variable.
- The current iteration is skipped, and the loop continues with the next iteration.
- The loop restarts with the loop variable reset.
Answer: C) The current iteration is skipped, and the loop continues with the next iteration.
Explanation: The continue statement skips the rest of the code in the current iteration and moves to the next iteration of the loop.
Question 13: Which of the following code snippets will print numbers from 1 to 5 except 3? (Choose all that apply)
for i in range(1, 6): if i == 3: continue print(i)
for i in range(1, 6): if i != 3: print(i)
for i in range(1, 6): if i == 3: break print(i)
for i in range(1, 6): if i == 3: continue else: print(i)
Answer: A), B), and D)
Explanation: Options A,B, and D correctly use continue to skip printing 3 and print all other numbers. Option B prints all numbers, including 3. Option C stops the loop before printing any numbers greater than 2.
Question 14: Arrange the steps to create a loop that stops when it encounters a negative number and prints all non-negative numbers.
- Check if the loop variable is negative.
- Use the break statement to exit the loop if a negative number is found.
- Print the loop variable if it is non-negative.
- Continue looping through the list of numbers.
Answer: 1, 2, 3, 4
Explanation: First, check if the loop variable is negative. If it is, use break to exit the loop. Otherwise, print the number and continue looping.
Question 15: Complete the following code to skip the current iteration if x is less than 0.
for x in [-3, 0, 2, -1, 4]: if x < 0: ______ print(x)▼
Answer: continue
Explanation: The continue statement skips the current iteration if x is less than 0, so only non-negative numbers are printed.
Question 16: What will be the output of the following code?
for i in range(1, 6): if i == 4: continue print(i) else: print("Done")
- 1 2 3 4 5 Done
- 1 2 3 5 Done
- 1 2 3 4 Done
- 1 2 3 5
Answer: B) 1 2 3 5 Done
Explanation: The continue statement skips the iteration when i == 4, so 4 is not printed. The loop continues, and the else block is executed, printing "Done."
Question 17: Insert the correct statement to exit the loop if the sum of i and j equals 5.
for i in range(3): for j in range(3): if i + j == 5: ______ print(i, j)▼
Answer: break
Explanation: The break statement is used to exit the inner loop if the sum of i and j equals 5. However, in this case, i + j will never equal 5, so the loop will run to completion.
Question 18: What will happen if a break statement is used in the inner loop of the following nested loop?
for i in range(3): for j in range(3): if j == 1: break print(i, j)
- The outer loop terminates.
- The inner loop terminates, and the outer loop continues.
- Both loops terminate.
- The loop runs indefinitely.
Answer: B) The inner loop terminates, and the outer loop continues.
Explanation: The break statement exits the inner loop when j == 1, but the outer loop continues with the next iteration.
Question 19: In which of the following situations would you use a continue statement? (Choose all that apply)
- When you want to skip processing certain elements in a loop.
- When you want to terminate the loop early.
- When you want to continue looping but skip specific iterations.
- When you want to restart the loop from the beginning.
Answer: A) and C)
Explanation: The continue statement is used to skip certain iterations within a loop and continue with the next iteration.
Question 20: Arrange the steps to create a loop that prints only the first 3 elements of a list.
- Use the break statement to exit the loop after printing 3 elements.
- Initialize a counter to keep track of the number of elements printed.
- Increment the counter after each print.
- Print the current element.
Answer: 2, 4, 3, 1
Explanation: First, initialize the counter, then print the current element, increment the counter, and finally use break to exit the loop after printing 3 elements
Question 21: Complete the following code to exit the loop when the sum of x and y is greater than 10.
x = 5 for y in range(10): if x + y > 10: ______ print(x, y)▼
Answer: break
Explanation: The break statement is used to exit the loop when the sum of x and y exceeds 10.
Question 22: What will be the output of the following code?
for i in range(4): if i % 2 == 0: continue print(i) else: print("Done")
- 0 2 Done
- 1 3 Done
- 0 1 2 3
- 1 3
Answer: B) 1 3 Done
Explanation: The continue statement skips even numbers (0 and 2), so only 1 and 3 are printed. The else block is executed, printing "Done."
Question 23: Insert the correct statement to skip the current iteration when i is equal to 5.
for i in range(10): if i == 5: ______ print(i)▼
Answer: continue
Explanation: The continue statement skips the iteration when i == 5, so 5 is not printed, but the loop continues with the next iteration.
Question 24: What is the effect of a break statement in a loop?
- It skips the rest of the loop's code for the current iteration and continues with the next iteration.
- It exits the loop and stops further iterations.
- It restarts the loop from the beginning.
- It pauses the loop temporarily.
Answer: B) It exits the loop and stops further iterations
Explanation: The break statement exits the loop entirely, stopping any further iterations.
Question 25: Which of the following loops will print numbers from 1 to 10 except for 4? (Choose all that apply)
for i in range(1, 11): if i == 4: continue print(i)
for i in range(1, 11): if i != 4: print(i)
for i in range(1, 11): if i == 4: break print(i)
for i in range(1, 11): if i == 4: continue else: print(i)
Answer: A), B), and D)
Explanation: Options A, B, and D use continue to skip printing 4, so the loop prints all other numbers. Option B prints all numbers, including 4. Option C stops the loop before printing any number greater than 3.
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python/certificate/control-flow-conditional-statements-controlling-loop-execution-with-break-and-continue.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics