w3resource

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

  1. It repeats as long as the condition is True.
  2. It executes at least once, even if the condition is False from the start.
  3. It can create an infinite loop if the condition never becomes False.
  4. It always runs a fixed number of iterations

Answer:

  1. It repeats as long as the condition is True.
  2. 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=" ")
  1. 2 3 4
  2. 2 3 4 5
  3. 3 4 5
  4. 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=" ")
  1. Prints each letter of the string "Python" on a new line
  2. Prints "Python" as a whole
  3. Prints each letter of "Python" separated by a space
  4. 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">
  • range(5, 8)
  • range(5, 7)
  • range(4, 8)
  • range(5, 8, 1)
  • 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.

    1. Print the value of x
    2. Initialize x to 1
    3. Increment x by 1
    4. 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. 1 2 3 4 5 6 7 8 9
    2. 1 3 6 9
    3. 1 4 7
    4. 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
    
    1. Prints 0 1 2 3 4 5
    2. Prints 5 4 3 2 1
    3. Runs indefinitely
    4. 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)

    1. for i in range(len(my_list)):
    2. while i < len(my_list):
    3. for item in my_list:
    4. for i in range(0, len(my_list), 2):

    Answer:

    1. for i in range(len(my_list)):
    2. for item in my_list:
    3. while i < len(my_list):
    4. 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.

    1. Use the print function to print each key
    2. Create a for loop to iterate over my_dict
    3. 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=" ")
    
    1. 5 10 15
    2. 5 10
    3. 5 10 15 20
    4. 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
    
    1. It prints 0 1 2 3
    2. It prints 3 2 1
    3. It runs indefinitely
    4. 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)

    1. range(0, 5)
    2. range(5)
    3. range(0, 5, 1)
    4. range(1, 5)

    Answer:

    1. range(0, 5)
    2. range(5)
    3. 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.

    1. Use the print function to print the value of i
    2. Create a for loop using range(0, 9, 2)
    3. 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)
    
    1. 8
    2. 10
    3. 16
    4. 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)
    
    1. Checks if element exists in my_list
    2. Iterates over each element in my_list
    3. Raises a TypeError if my_list is not a list
    4. 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)

    1. It generates numbers up to, but not including, the stop value.
    2. It can take a step value to increment by a number other than 1.
    3. It can only be used in for loops.
    4. 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.

    

    Become a Patron!

    Follow us on Facebook and Twitter for latest update.