# Python PCEP Exam - Nested Loops and Conditional Statements

## 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 "nesting loops and conditional statements." 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)
```
1. 0 0, 1 1, 2 2
2. 0 1, 1 2, 2 3
3. 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
4. 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)

1. The inner loop runs to completion for each iteration of the outer loop.
2. Both loops must use the same loop variable.
3. The inner loop can use a different loop variable than the outer loop.
4. The number of iterations of the inner loop depends on the outer loop.

1. The inner loop runs to completion for each iteration of the outer loop.
2. The inner loop can use a different loop variable than the outer loop.
3. 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, ______)
```

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)
```
1. 0 0, 1 1, 2 2
2. 0 0, 1 1
3. 1 1, 2 2
4. 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)
```

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")
```
1. Prints "Equal" for all combinations of i and j
2. Prints "Equal" only when i equals j, otherwise prints "Not equal"
3. Prints "Not equal" for all combinations of i and j
4. 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)

1. The inner loop terminates and the outer loop continues.
2. Both the inner and outer loops terminate immediately.
3. Only the outer loop terminates, and the inner loop continues.
4. The break statement can only terminate the loop in which it is placed.

1. The inner loop terminates and the outer loop continues.
2. 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.

1. Use a nested loop to iterate over rows and columns.
2. Print the current value of the row and column.
3. Define the range for rows and columns.

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, ______)
```

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)
```
1. 0 0, 0 1, 0 2, 1 0, 1 1, 1 2
2. 0 0, 0 1, 0 2, 1 0, 1 1
3. 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
4. 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")
______
```

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?

1. The inner loop immediately terminates.
2. The outer loop immediately terminates.
3. The current iteration of the inner loop is skipped, and the outer loop continues.
4. 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)

1. Whether to enter the inner loop.
2. Whether to terminate the inner loop prematurely.
3. Whether to skip a specific iteration of the outer loop.
4. Whether to exit the outer loop.

1. Whether to enter the inner loop.
2. Whether to terminate the inner loop prematurely.
3. Whether to skip a specific iteration of the outer loop.
4. 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.

1. Initialize a counter to 0.
2. Increment the counter if i < j.
3. Use a nested loop to iterate over i and j.
4. Print the final count.

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")
```

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)
```
1. 0 0, 0 1, 0 2, 1 0, 1 2, 2 0, 2 1, 2 2
2. 0 0, 0 1, 0 2, 1 1, 2 0, 2 1, 2 2
3. 0 0, 0 1, 1 1, 1 2, 2 0, 2 1, 2 2
4. 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)
```

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)
```
1. The outer loop terminates.
2. The inner loop terminates, and the outer loop continues.
3. Both loops terminate.
4. 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)

1. Generating combinations of two sequences.
2. Checking multiple conditions for each element in a sequence.
3. Controlling the flow of loops based on complex conditions.
4. Skipping iterations or terminating loops early.

1. Generating combinations of two sequences.
2. Checking multiple conditions for each element in a sequence.
3. Controlling the flow of loops based on complex conditions.
4. 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.

1. Use a nested loop to compare elements from both lists.
2. Print "Duplicate found" if a match is found.
3. Iterate over each element in the first list.
4. Compare each element in the first list with each element in the second list.

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("______")
```

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
```
1. 0 0, 1 1, 2 2
2. 0 0, 1 1
3. 0 0, 0 1, 1 0, 1 1, 2 0, 2 1
4. No output

Answer: B) 0 0, 1 1

Explanation: Since a is less than b, the elif block is executed, printing "b is greater."

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)
______
```

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)
```
1. 0 0, 0 1, 1 0, 1 1, 1 2, 2 0, 2 1
2. 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1
3. 0 0, 0 1, 1 0, 1 1, 2 0
4. 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)

1. for i in range(3): for j in range(3): if i > j: print(i, j)
2. while i < 3: while j < 3: if i == j: print(i, j)
3. for i in range(2): while j < 2: if i + j == 1: print(i, j)
4. for i in range(3): for j in range(2): if i < j: print(i, j)