w3resource

Python PCEP Exam - Iterating Through Sequences

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 will be the output of the following code?

sequence = [1, 2, 3]
for item in sequence:
    print(item)
  1. 1 2 3
  2. [1, 2, 3]
  3. item
  4. None

Answer: A) 1 2 3

Explanation: The for loop iterates over each element in the list sequence and prints them one by one, resulting in 1 2 3.

Question 2: Which of the following sequences can be iterated through using a for loop? (Choose all that apply)

  1. A list
  2. A string
  3. A tuple
  4. An integer

Answer:

  1. A list
  2. A string
  3. A tuple

Explanation: Lists, strings, and tuples are all iterable sequences in Python. An integer is not iterable.

Question 3: Complete the following code to iterate over the characters in a string called text.

text = "Python"
for ______ in text:
    print(______ , end=" ")

Answer: char, char

Explanation: The loop variable char will take each character from the string text one by one, and it will be printed followed by a space.

Question 4: What will be the output of the following code?

sequence = (10, 20, 30)
for i in sequence:
    print(i, end=", ")
  1. 10 20 30
  2. 10, 20, 30,
  3. 10, 20, 30,
  4. 10 20 30,

Answer: B) 10, 20, 30,

Explanation: The tuple sequence is iterated over, and each element is printed with a comma and a space following it. The last comma and space are also printed because they are part of the print function's end argument.

Question 5: Insert the appropriate keyword to iterate over a list called numbers.

numbers = [5, 10, 15]
for ______ in numbers:
    print(______ )

Answer: item, item

Explanation: The for loop iterates over each element in the list numbers, and each element is assigned to the variable item and then printed.

Question 6: What does the following code do?

for letter in "Hello":
    print(letter)
  1. Prints each word in the string "Hello"
  2. Prints each letter in the string "Hello" on a new line
  3. Raises a TypeError
  4. Does nothing

Answer: B) Prints each letter in the string "Hello" on a new line

Explanation: The loop iterates over each character in the string "Hello", printing each one on a new line.

Question 7: Which of the following statements are correct regarding iteration over a dictionary? (Choose all that apply)

ol type="A">
  • You can iterate over the keys using for key in my_dict:.
  • You can iterate over the values using for value in my_dict:.
  • You can iterate over the key-value pairs using for key, value in my_dict.items():.
  • You can only iterate over the keys, not the values.
  • Answer: A) You can iterate over the keys using for key in my_dict:.
    C) You can iterate over the key-value pairs using for key, value in my_dict.items():.

    Explanation: In a dictionary, you can iterate over the keys directly or over both keys and values using the items() method. Iterating over the values requires using my_dict.values().

    Question 8: Arrange the steps to correctly iterate over the elements of a list called fruits and print each element in uppercase.

    1. Create the for loop to iterate over fruits
    2. Print each element in uppercase
    3. Access each element using the loop variable

    Answer: 1, 3, 2

    Explanation: First, you create the for loop to iterate over fruits, then access each element, and finally, print each element in uppercase.

    Question 9: Complete the following code to iterate over a dictionary called my_dict and print each key.

    my_dict = {"a": 1, "b": 2, "c": 3}
    for ______ in my_dict:
        print(______ )
    

    Answer: key, key

    Explanation: The loop iterates over the keys of my_dict, and each key is printed.

    Question 10: What will be the output of the following code?

    my_tuple = (1, 2, 3)
    for i in my_tuple:
        print(i, end="-")
    
    1. 1-2-3
    2. 1 2 3
    3. 1-2-3-
    4. 1, 2, 3

    Answer: C) 1-2-3-

    Explanation: The tuple my_tuple is iterated over, and each element is printed with a hyphen following it, including a trailing hyphen after the last element.

    Question 11: Insert the appropriate keyword to iterate over both the keys and values in a dictionary called info.

    info = {"name": "Alice", "age": 30}
    for key, value in ______:
        print(key, ":", value)
    

    Answer: info.items()

    Explanation: The items() method returns a view object that displays a list of dictionary's key-value tuple pairs, allowing you to iterate over both keys and values.

    Question 12: What is the correct way to iterate through the characters of the string "Python" and print them in reverse order?

    1. for char in "Python"[::-1]:
    2. for char in "Python":
    3. for char in reversed("Python"):
    4. Both A and C

    Answer: D) Both A and C

    Explanation: The slice "Python"[::-1] reverses the string, and the reversed() function also reverses the string. Both approaches are correct for iterating through the string in reverse order.

    Question 13: Which of the following methods can be used to iterate over the indices of a list? (Choose all that apply)

    1. Using the range() function with the length of the list
    2. Using the enumerate() function
    3. Using the zip() function
    4. Using a for loop directly on the list

    Answer:A) Using the range() function with the length of the list
    B) Using the enumerate() function

    Explanation: The range() function with the length of the list can generate indices, and enumerate() provides both the index and the element. The zip() function is used for pairing elements from multiple sequences, not for iterating over indices directly.

    Question 14: Arrange the steps to iterate over a dictionary student and print both keys and values.

    1. Use for loop to iterate over student.items()
    2. Print the key-value pair
    3. Assign key, value in each iteration

    Answer: 1, 3, 2

    Explanation: First, create the loop with student.items(), then assign key, value in each iteration, and finally print the key-value pair.

    Question 15: Complete the following code to iterate over the list of tuples coordinates and print each pair.

    coordinates = [(1, 2), (3, 4), (5, 6)]
    for ______ in coordinates:
        print(______ )
    

    Answer: pair, pair

    Explanation: The loop variable pair will take each tuple from the list coordinates, and each pair is printed.

    Question 16: What will be the output of the following code?

    names = ["John", "Paul", "George", "Ringo"]
    for i, name in enumerate(names):
        print(i, name)
    
    1. John Paul George Ringo
    2. 0 John, 1 Paul, 2 George, 3 Ringo
    3. 0 John 1 Paul 2 George 3 Ringo
    4. John 0 Paul 1 George 2 Ringo 3

    Answer: B) 0 John, 1 Paul, 2 George, 3 Ringo

    Explanation: The enumerate() function provides both the index i and the element name, which are printed together.

    Question 17: Insert the appropriate statement to print each value in the list data doubled.

    data = [2, 4, 6]
    for value in data:
        ______
    

    Answer: print(value * 2)

    Explanation: The loop iterates over each element in the list data and prints the element multiplied by 2.

    Question 18: Which of the following correctly iterates over the elements of two lists a and b in parallel?

    1. for i in range(len(a)):
    2. for i in a and b:
    3. for x, y in zip(a, b):
    4. for i in a or b:

    Answer: C) for x, y in zip(a, b):

    Explanation: The zip() function pairs elements from both lists a and b together, allowing them to be iterated over in parallel.

    Question 19: Which of the following loops will correctly iterate over a list of dictionaries called data? (Choose all that apply)

    1. for entry in data:
    2. for key, value in data:
    3. for entry in data.keys():
    4. for entry in data.values():

    Answer:A) for entry in data:
    D) for entry in data.values():

    Explanation: The first loop correctly iterates over each dictionary in the list. The last loop is incorrect because data is a list of dictionaries, not a dictionary.

    Question 20: Arrange the steps to correctly iterate over a string s and print each character on the same line separated by hyphens.

    1. Create the for loop to iterate over s
    2. Use print() with end="-" to print each character followed by a hyphen
    3. Access each character using the loop variable

    Answer: 1, 3, 2

    Explanation: First, create the loop to iterate over s, then access each character, and finally, print each character followed by a hyphen.

    Question 21: Complete the following code to iterate over the items in a list of strings words and print them in uppercase.

    words = ["apple", "banana", "cherry"]
    for ______ in words:
        print(______ )
    

    Answer: word, word.upper()

    Explanation: The loop variable word iterates over each string in words, and word.upper() converts each string to uppercase before printing.

    Question 22: What will be the output of the following code?

    numbers = [1, 2, 3, 4, 5]
    result = 0
    for num in numbers:
        result += num
    print(result)
    
    1. 10
    2. 15
    3. 5
    4. 0

    Answer: B) 15

    Explanation: The loop iterates over each element in numbers, adding them to result. The final sum, 15, is printed.

    Question 23: Insert the correct statement to iterate over the indices and elements of a list colors.

    colors = ["red", "green", "blue"]
    for ______:
        print(i, color)
    

    Answer: i, color in enumerate(colors)

    Explanation: The enumerate() function returns both the index i and the element color, allowing them to be printed together.

    Question 24: Which of the following correctly iterates through a sequence in steps of 2?

    1. for i in range(0, len(sequence), 2):
    2. for i in range(0, len(sequence), 3):
    3. for i in range(2, len(sequence), 2):
    4. for i in range(2, len(sequence)):

    Answer: A) for i in range(0, len(sequence), 2):

    Explanation: The range() function with a step value of 2 iterates through every second element of the sequence, starting at 0.

    Question 25: Which of the following sequences can be iterated over using the in keyword? (Choose all that apply)

    1. A string
    2. A dictionary
    3. A list
    4. A set

    Answer:

    1. A string
    2. A list
    3. A dictionary
    4. A set

    Explanation: The in keyword can be used to iterate over all these sequences in Python: strings, lists, dictionaries, and sets.

    Test your Python skills with w3resource's quiz

    

    Follow us on Facebook and Twitter for latest update.