PCEP Exam - Control Flow in Python: Mastering Conditional Statements and Loops
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 "building loops with while, for, range(), and in." 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 is the output of the following code?
for i in range(3): print(i)
- 0 1 2
- 0 1 2 3
- 1 2 3
- 1 2 3 4
Answer: A) 0 1 2
Explanation: The range(3) function generates a sequence of numbers starting from 0 up to, but not including, 3, so the output will be 0 1 2.
Question 2: Which of the following statements are correct about the while loop? (Choose all that apply)
- It repeats as long as the condition is True.
- It executes at least once, even if the condition is False from the start.
- It can create an infinite loop if the condition never becomes False.
- It always runs a fixed number of iterations
Answer:
- It repeats as long as the condition is True.
- It can create an infinite loop if the condition never becomes False.
Explanation: The while loop continues to execute as long as the condition remains True. If the condition never becomes False, the loop can run indefinitely. Unlike the for loop, it does not necessarily run a fixed number of times.
Question 3: Complete the following code to create a while loop that runs as long as x is less than 5.
x = 0 while ______: print(x) x += 1▼
Answer: x < 5
Explanation: The loop continues to run while x is less than 5, printing the value of x and then incrementing it by 1 in each iteration.
Question 4: What will be the output of the following code?
for i in range(2, 5): print(i, end=" ")
- 2 3 4
- 2 3 4 5
- 3 4 5
- 2 4 5
Answer: A) 2 3 4
Explanation: The range(2, 5) function generates numbers starting from 2 up to, but not including, 5. The loop prints these numbers on the same line separated by spaces.
Question 5: Insert the correct keyword to terminate a loop prematurely when a condition is met.
for i in range(10): if i == 3: ______ print(i)▼
Answer: break
Explanation: The break statement is used to exit the loop when the condition i == 3 is met. The loop will terminate early, and the subsequent iterations will not occur.
Question 6: What does the following code do?
for letter in "Python": print(letter, end=" ")
- Prints each letter of the string "Python" on a new line
- Prints "Python" as a whole
- Prints each letter of "Python" separated by a space
- Raises a TypeError
Answer: C) Prints each letter of "Python" separated by a space
Explanation: The loop iterates over each character in the string "Python" and prints it followed by a space.
Question 7: Which of the following ranges correctly produce the sequence [5, 6, 7]? (Choose all that apply)
ol type="A">Answer: A) range(5, 8) D) range(5, 8, 1)
Explanation: The range(5, 8) generates numbers starting from 5 up to, but not including, 8, which results in the sequence [5, 6, 7]. The range(5, 8, 1) does the same, with an explicit step size of 1.
Question 8: Sort the steps to correctly create a while loop that prints numbers from 1 to 5.
- Print the value of x
- Initialize x to 1
- Increment x by 1
- Create the while loop with the condition x <= 5
Answer: 2, 4, 1, 3
Explanation: First, initialize x to 1, then create the while loop with the condition x <= 5. Inside the loop, print x and then increment it by 1.
Question 9: Complete the following code to create a for loop that prints the numbers 1 to 4.
for i in ______: print(i)▼
Answer: range(1, 5)
Explanation: The range(1, 5) generates numbers starting from 1 up to, but not including, 5, so the loop prints 1, 2, 3, and 4.
Question 10: What will be the output of the following code?
for i in range(1, 10, 3): print(i, end=" ")
- 1 2 3 4 5 6 7 8 9
- 1 3 6 9
- 1 4 7
- 3 6 9
Answer: C) 1 4 7
Explanation: The range(1, 10, 3) starts at 1, ends before 10, and increments by 3 on each iteration, producing the sequence 1, 4, 7.
Question 11: Insert the appropriate keyword to skip the current iteration when i is 2.
for i in range(5): if i == 2: ______ print(i)▼
Answer: continue
Explanation: The continue statement skips the rest of the code inside the loop for the current iteration when i equals 2, so 2 will not be printed.
Question 12: What does the following while loop do?
x = 5 while x > 0: print(x) x -= 1
- Prints 0 1 2 3 4 5
- Prints 5 4 3 2 1
- Runs indefinitely
- Does nothing
Answer: B) Prints 5 4 3 2 1
Explanation: The loop starts with x as 5 and decreases x by 1 each time it prints, stopping when x becomes 0.
Question 13: Which of the following loops will correctly iterate over a list called my_list? (Choose all that apply)
- for i in range(len(my_list)):
- while i < len(my_list):
- for item in my_list:
- for i in range(0, len(my_list), 2):
Answer:
- for i in range(len(my_list)):
- for item in my_list:
- while i < len(my_list):
- for i in range(0, len(my_list), 2):
Explanation: All these loops correctly iterate over the elements of my_list, though they do so in different ways. The first, second, and third loop iterate over all elements, while the fourth iterates over every second element.
Question 14: Arrange the steps to correctly use a for loop to iterate over the keys in a dictionary called my_dict.
- Use the print function to print each key
- Create a for loop to iterate over my_dict
- Access each key in my_dict using the in keyword
Answer: 2, 3, 1
Explanation: First, create the for loop to iterate over the dictionary. Then, access each key using the in keyword, and finally, print the keys.
Question 15: Complete the following code to iterate over the elements of a list called numbers using a for loop.
numbers = [1, 2, 3, 4, 5] for num in ______: print(num)▼
Answer: numbers
Explanation: The loop iterates over the elements of the numbers list and prints each element.
Question 16: What will be the output of the following code?
for i in range(5, 15, 5): print(i, end=" ")
- 5 10 15
- 5 10
- 5 10 15 20
- 5 10 15 20 25
Answer: B) 5 10
Explanation: The range(5, 15, 5) starts at 5, ends before 15, and increments by 5, producing the sequence 5 10.
Question 17: Insert the appropriate statement to ensure that the loop prints "Done" after finishing.
for i in range(3): print(i) # Insert statement here▼
Answer: print("Done")
Explanation: After the loop completes, the print("Done") statement will execute, indicating that the loop has finished.
Question 18: What is the behavior of the following while loop?
n = 3 while n: print(n) n -= 1
- It prints 0 1 2 3
- It prints 3 2 1
- It runs indefinitely
- It raises an error
Answer: B) It prints 3 2 1
Explanation: The loop continues as long as n is non-zero. Starting from 3, it prints 3, then decrements n by 1 on each iteration, resulting in 3 2 1.
Question 19: Which of the following will correctly produce a sequence from 0 to 4 using the range() function? (Choose all that apply)
- range(0, 5)
- range(5)
- range(0, 5, 1)
- range(1, 5)
Answer:
- range(0, 5)
- range(5)
- range(0, 5, 1)
Explanation: The range(0, 5), range(5), and range(0, 5, 1) all produce the sequence [0, 1, 2, 3, 4]. The range(1, 5) starts at 1, so it doesn't include 0.
Question 20: Arrange the steps to correctly create a for loop that prints every second number from 0 to 8.
- Use the print function to print the value of i
- Create a for loop using range(0, 9, 2)
- Set the end value to 8 in the range function
Answer: 2, 3, 1
Explanation: First, create the for loop using range(0, 9, 2) to start at 0 and increment by 2, then print each value of i.
Question 21: Complete the following code to create an infinite loop.
while ______: print("Running")▼
Answer: True
Explanation: The loop continues indefinitely because True is always true, creating an infinite loop.
Question 22: What will be the output of the following code?
x = 1 while x < 10: x *= 2 print(x)
- 8
- 10
- 16
- 9
Answer: 16
Explanation: The loop multiplies x by 2 until x reaches or exceeds 10. The final value of x is 16, which is printed.
Question 23: Insert the correct keyword to stop the loop from continuing indefinitely.
while True: print("Looping") ______▼
Answer: break
Explanation: The break statement is used to exit the infinite loop, stopping the loop from continuing indefinitely.
Question 24: What does the in keyword do in the following loop?
for element in my_list: print(element)
- Checks if element exists in my_list
- Iterates over each element in my_list
- Raises a TypeError if my_list is not a list
- Creates a copy of my_list
Answer: B) Iterates over each element in my_list
Explanation: The in keyword is used in the loop to iterate over each element in my_list.
Question 25: Which of the following statements are correct about the range() function? (Choose all that apply)
- It generates numbers up to, but not including, the stop value.
- It can take a step value to increment by a number other than 1.
- It can only be used in for loops.
- It starts at 0 by default if no start value is provided.
Answer: A) It generates numbers up to, but not including, the stop value.
B) It can take a step value to increment by a number other than 1.
D) It starts at 0 by default if no start value is provided.
Explanation: The range() function generates numbers starting from the start value (or 0 if none is provided) up to, but not including, the stop value. It can take an optional step value to change the increment.
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-building-loops.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics