Python PCEP Exam: Loops - while-else and for-else Explained
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 "expanding loops with while-else and for-else." 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 will be the output of the following code?
for i in range(3): for j in range(2): print(i, j)
- 0 0, 1 1, 2 2
- 0 1, 1 2, 2 3
- 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
- 0 0, 1 0, 2 0
Answer: C) 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
Explanation: The outer loop runs three times, and for each iteration, the inner loop runs two times. The pairs (i, j) are printed accordingly.
Question 2: Which of the following statements are correct regarding nested loops? (Choose all that apply)
- The inner loop runs to completion for each iteration of the outer loop.
- Both loops must use the same loop variable.
- The inner loop can use a different loop variable than the outer loop.
- The number of iterations of the inner loop depends on the outer loop.
Answer: A) The inner loop runs to completion for each iteration of the outer loop.
C) The inner loop can use a different loop variable than the outer loop.
D) The number of iterations of the inner loop depends on the outer loop.
Explanation: The inner loop executes completely for each iteration of the outer loop. The loop variables can be different, and the inner loop may depend on the outer loop’s variable.
Question 3: Complete the following code to print all combinations of the numbers 1, 2, and 3 with the letters A and B.
for num in [1, 2, 3]: for letter in ["A", "B"]: print(num, ______)▼
Answer: letter
Explanation: The variable letter represents each element in the list ["A", "B"], and the code prints each combination of num and letter.
Question 4: What will be the output of the following code?
for i in range(2): for j in range(3): if i == j: print(i, j)
- 0 0, 1 1, 2 2
- 0 0, 1 1
- 1 1, 2 2
- 0 1, 1 2, 2 3
Answer: B) 0 0, 1 1
Explanation: The if condition i == j is true when i and j are equal. The pairs (0, 0) and (1, 1) satisfy this condition and are printed.
Question 5: Insert the appropriate statement to skip the current iteration of the inner loop when i + j equals 2.
for i in range(3): for j in range(3): if i + j == 2: ______ print(i, j)▼
Answer: continue
Explanation: The continue statement skips the rest of the code inside the inner loop for the current iteration when i + j == 2, so the print(i, j) statement is not executed in that case.
Question 6: What does the following nested loop with an if-else statement do?
for i in range(2): for j in range(2): if i == j: print("Equal") else: print("Not equal")
- Prints "Equal" for all combinations of i and j
- Prints "Equal" only when i equals j, otherwise prints "Not equal"
- Prints "Not equal" for all combinations of i and j
- Raises a SyntaxError
Answer:B) Prints "Equal" only when i equals j, otherwise prints "Not equal"
Explanation: The if-else statement checks whether i equals j. If they are equal, it prints "Equal"; otherwise, it prints "Not equal".
Question 7: In the context of nested loops with conditional statements, which of the following are possible outcomes when using the break statement? (Choose all that apply)
- The inner loop terminates and the outer loop continues.
- Both the inner and outer loops terminate immediately.
- Only the outer loop terminates, and the inner loop continues.
- The break statement can only terminate the loop in which it is placed.
Answer: A) The inner loop terminates and the outer loop continues.
D) The break statement can only terminate the loop in which it is placed.
Explanation: The break statement exits only the loop in which it is placed, causing the inner loop to terminate while the outer loop continues.
Question 8: Arrange the steps to create a nested loop that prints a 3x3 grid of numbers.
- Use a nested loop to iterate over rows and columns.
- Print the current value of the row and column.
- Define the range for rows and columns.
Answer: 3, 1, 2
Explanation: First, define the range for rows and columns (3x3), then create the nested loops to iterate over rows and columns, and finally, print the row and column values.
Question 9: Complete the following code to create a nested loop that prints pairs of numbers and their sum, but skips pairs where the sum is greater than 3.
for i in range(3): for j in range(3): if i + j > 3: continue print(i, j, ______)▼
Answer: i + j
Explanation: The variable i + j calculates the sum of i and j, which is then printed along with the pair of numbers.
Question 10: What will be the output of the following code?
for i in range(3): for j in range(3): if i + j == 4: break print(i, j)
- 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1
- 0 0, 0 1, 0 2, 1 0, 1 1
- 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
- 0 0, 0 1, 1 0, 1 1
Answer: A) 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1
Explanation: The break statement terminates the inner loop when i + j == 4, so the loop ends before it can print the pair (2, 2).
Question 11: Insert the appropriate code to ensure that the loop prints "Inner" for each iteration of the inner loop and "Outer" after each iteration of the outer loop.
for i in range(2): for j in range(2): print("Inner") ______▼
Answer: print("Outer")
Explanation: The print("Outer") statement is placed after the inner loop, ensuring it is executed after each complete iteration of the inner loop.
Question 12: What happens if a continue statement is used in the inner loop of a nested loop?
- The inner loop immediately terminates.
- The outer loop immediately terminates.
- The current iteration of the inner loop is skipped, and the outer loop continues.
- Both loops terminate immediately.
Answer: C) The current iteration of the inner loop is skipped, and the outer loop continues.
Explanation: The continue statement skips the remaining code in the current iteration of the inner loop and continues with the next iteration of the inner loop. The outer loop remains unaffected.
Question 13: In a nested loop with conditional statements, which of the following actions can be controlled using an if-else statement? (Choose all that apply)
- Whether to exit the outer loop.
- Whether to enter the inner loop.
- Whether to terminate the inner loop prematurely.
- Whether to skip a specific iteration of the outer loop.
Answer:
- Whether to enter the inner loop.
- Whether to terminate the inner loop prematurely.
- Whether to skip a specific iteration of the outer loop.
- Whether to exit the outer loop.
Explanation: An if-else statement can control all these actions by determining whether to execute the loop, whether to use break or continue statements, and whether to proceed with the current or next iteration.
Question 14: Arrange the steps to correctly implement a nested loop that counts the number of pairs (i, j) where i is less than j.
- Initialize a counter to 0.
- Increment the counter if i < j.
- Use a nested loop to iterate over i and j.
- Print the final count.
Answer: 1, 3, 2, 4
Explanation: First, initialize the counter, then use the nested loops to iterate over i and j, increment the counter when i < j, and finally, print the count.
Question 15: Complete the following code to create a nested loop that prints "Match" if both i and j are equal to 2, and "No match" otherwise.
for i in range(3): for j in range(3): if i == 2 and j == 2: print("______") else: print("No match")▼
Answer: Match
Explanation: The if statement checks if both i and j are 2. If true, it prints "Match"; otherwise, it prints "No match".
Question 16: What will be the output of the following code?
for i in range(3): for j in range(3): if i == 1 and j == 1: continue print(i, j)
- 0 0, 0 1, 0 2, 1 0, 1 2, 2 0, 2 1, 2 2
- 0 0, 0 1, 0 2, 1 1, 2 0, 2 1, 2 2
- 0 0, 0 1, 1 1, 1 2, 2 0, 2 1, 2 2
- 0 0, 0 1, 0 2, 1 0, 1 1, 2 0, 2 1, 2 2
Answer: A) 0 0, 0 1, 0 2, 1 0, 1 2, 2 0, 2 1, 2 2
Explanation: The continue statement skips the print(i, j) statement when i == 1 and j == 1, so the pair (1, 1) is not printed.
Question 17: Insert the correct statement to ensure that the loop prints "Breaking inner loop" and breaks out of the inner loop when i + j equals 3.
for i in range(4): for j in range(4): if i + j == 3: print("______") break print(i, j)▼
Answer: Breaking inner loop
Explanation: The code should print "Breaking inner loop" when i + j == 3, then break out of the inner loop.
Question 18: What will happen if the break statement is used in the inner loop of the following nested loop?
for i in range(2): for j in range(2): 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 to the next iteration.
Question 19: Which of the following are valid uses of nested loops with conditional statements? (Choose all that apply)
- Controlling the flow of loops based on complex conditions.
- Generating combinations of two sequences.
- Checking multiple conditions for each element in a sequence.
- Skipping iterations or terminating loops early.
Answer:
- Generating combinations of two sequences.
- Checking multiple conditions for each element in a sequence.
- Controlling the flow of loops based on complex conditions.
- Skipping iterations or terminating loops early.
Explanation: Nested loops with conditional statements are versatile and can be used for generating combinations, checking conditions, controlling flow, and managing loop execution.
Question 20: Arrange the steps to correctly implement a nested loop that checks for duplicate elements in two lists and prints "Duplicate found" if any are found.
- Use a nested loop to compare elements from both lists.
- Print "Duplicate found" if a match is found.
- Iterate over each element in the first list.
- Compare each element in the first list with each element in the second list.
Answer: 3, 4, 1, 2
Explanation: First, iterate over each element in the first list, then compare it with elements in the second list using a nested loop, and print "Duplicate found" if a match is found.
Question 21: Complete the following code to create a nested loop that checks if any element in the list A is greater than any element in the list B and prints "Found" if so.
A = [1, 2, 3] B = [0, 4, 5] for a in A: for b in B: if a > b: print("______")▼
Answer: Found
Explanation: The if statement checks if a > b, and if true, it prints "Found".
Question 22: What will be the output of the following code?
for i in range(3): for j in range(2): if i == j: print(i, j) else: continue
- 0 0, 1 1, 2 2
- 0 0, 1 1
- 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
- No output
Answer: B) 0 0, 1 1
Explanation: The code prints (i, j) pairs where i == j. The else block contains continue, which skips the current iteration of the loop, so only pairs (0, 0) and (1, 1) are printed.
Question 23: Insert the correct statement to print "Outer loop" after each iteration of the outer loop, regardless of whether the inner loop completes or breaks.
for i in range(2): for j in range(2): if j == 1: break print(i, j) ______▼
Answer: print("Outer loop")
Explanation: The print("Outer loop") statement is placed after the inner loop to ensure it runs after each iteration of the outer loop, regardless of the inner loop’s completion.
Question 24: What is the output of the following nested loop with a conditional statement?
for i in range(3): for j in range(3): if i + j > 3: break print(i, j)
- 0 0, 0 1, 1 0, 1 1, 1 2, 2 0, 2 1
- 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1
- 0 0, 0 1, 1 0, 1 1, 2 0
- 0 0, 0 1, 1 0, 1 1, 1 2
Answer: B) 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1
Explanation: The break statement terminates the inner loop when i + j > 3, so some pairs are not printed.
Question 25: Which of the following loops correctly nest and utilize conditional statements? (Choose all that apply)
- for i in range(3): for j in range(3): if i > j: print(i, j)
- while i < 3: while j < 3: if i == j: print(i, j)
- for i in range(2): while j < 2: if i + j == 1: print(i, j)
- for i in range(3): for j in range(2): if i < j: print(i, j)
Answer: A) for i in range(3): for j in range(3): if i > j: print(i, j)
D) for i in range(3): for j in range(2): if i < j: print(i, j)
Explanation: Options A and D are correct because they properly nest loops and utilize conditional statements to control the flow. Option B lacks initialization and increment for i and j, and Option C mixes for and while loops incorrectly.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics