PCEP Practice Test: Initializing Loops in Python Lists
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 questions focusing on the topic "initializing loops" specifically for lists in Python, designed for the PCEP-30-02 certification exam. The questions cover various formats including single-select, multiple-select, gap fill, code insertion, sorting, and "rearrange" style questions. Each question is followed by the correct answer and explanation.
Question 1: Which of the following is the correct way to initialize a loop to iterate through a list named my_list?
- while item in my_list:
- for i in my_list:
- for i in range(len(my_list))
- while i < len(my_list):
Answer: b) for i in my_list:
Explanation: The correct way to initialize a loop to iterate directly through the elements of a list is for i in my_list:
Question 2: What is the output of the following code?
numbers = [1, 2, 3] for number in numbers: print(number)
- 1 2 3
- o b) 0 1 2
- [1, 2, 3]
- Error
Answer: a) 1 2 3
Explanation: The loop iterates through the list and prints each element in order.
Question 3: Which of the following initializes a loop to iterate through the indices of a list?
- for index in range(len(my_list)):
- for index in my_list:
- while index in my_list:
- while index < len(my_list):
Answer: a) for index in range(len(my_list)):
Explanation: The range(len(my_list)) expression generates indices from 0 to the length of the list minus 1.
Question 4: Which of the following code snippets correctly initialize loops to iterate through all elements in a list? (Choose all that apply)
- for element in my_list:
- for i in range(len(my_list)):
- while i < len(my_list):
- for i in my_list:
Answer: a) for element in my_list:, b) for i in range(len(my_list)):
Explanation: Option a) iterates directly through the elements, and option b) iterates through the indices to access the elements.
Question 5: Which of the following can be used to initialize a loop to iterate through a list and print each element? (Choose two)
- for item in range(len(my_list)): print(my_list[item])
- for item in my_list: print(item)
- while i in range(len(my_list)): print(my_list[i])
- while item < len(my_list): print(my_list[item])
Answer: a) for item in range(len(my_list)): print(my_list[item]), b) for item in my_list: print(item).
Explanation: Option a) uses the indices to access each element, while option b) directly iterates through the elements.
Question 6: The loop for __________ in range(len(my_list)): is used to iterate over each index of my_list.
▼Answer: i
Explanation: The variable i is typically used as an index when iterating through a list with range(len(my_list)).
Question 7: The loop for __________ in my_list: is the simplest way to iterate over each element in a list.
▼Answer: item
Explanation: The loop variable item takes the value of each element in the list as it iterates through it.
Question 8: Arrange the following code statements in the correct order to initialize a loop and iterate through a list by index:
- for i in range(len(my_list)):
- print(my_list[i])
- my_list = ["a", "b", "c"]
Answer: c) my_list = ["a", "b", "c"]
a) for i in range(len(my_list)):
b) print(my_list[i])
Explanation: The list is defined first, then the loop is initialized using indices, and finally, each element is printed.
Question 9: Arrange the following code snippets in the correct order to initialize a loop and print each element of a list:
- my_list = [5, 10, 15]
- print(number)
- for number in my_list:
Answer: a) my_list = [5, 10, 15]
c) for number in my_list:
b) print(number)
Explanation: The list is created first, followed by the loop to iterate through it, and each element is printed.
Question 10: Complete the code to initialize a loop that prints each element in the list colors:
colors = ["red", "green", "blue"] for __________: print(color)▼
Answer: color in colors
Explanation: The correct loop is for color in colors: to iterate over each element in the list and print it.
Question 11: Complete the code to initialize a loop that iterates through a list by index and prints each element in reverse order:
items = [1, 2, 3, 4] for __________: print(items[i])▼
Answer: i in range(len(items)-1, -1, -1)
Explanation: The loop for i in range(len(items)-1, -1, -1) initializes a loop that iterates through the indices in reverse order.
Question 12: Insert the correct code to initialize a loop that prints both the index and value of each element in the list names:
names = ["Alice", "Bob", "Charlie"] __________ print(index, value)▼
Answer: for index, value in enumerate(names):
Explanation: The enumerate() function initializes a loop that provides both index and value during iteration.
Question 13: Insert the correct code to initialize a loop that prints only the elements in numbers that are greater than 10:
numbers = [5, 15, 20, 3, 12] __________ if num > 10: print(num)▼
Answer: for num in numbers:
Explanation: The loop for num in numbers: initializes a loop that iterates over each element in the list.
Question 14: Rearrange the code to initialize a loop that prints each element in the list items:
- print(item)
- items = [2, 4, 6]
- for item in items:
Answer: b) items = [2, 4, 6]
c) for item in items:
a) print(item)
Explanation: The list is defined first, followed by the loop initialization, and then the elements are printed.
Question 15: Organize the steps to initialize a loop that iterates through a list of cities and prints each one:
- print(city)
- for city in cities:
- cities = ["New York", "London", "Tokyo"]
Answer: c) cities = ["New York", "London", "Tokyo"]
b) for city in cities:
a) print(city)
Explanation: The list is created first, followed by the loop initialization, and each element is printed.
Question 16: What does the following code output?
nums = [10, 20, 30] for num in nums: print(num * 2)
- 20 40 60
- 10 20 30
- [20, 40, 60]
- Error
Answer: a) 20 40 60
Explanation: The loop multiplies each element by 2 and prints the result.
Question 17: What does the following code output?
cities = ["Paris", "Berlin", "Rome"] for i in range(len(cities)): print(cities[i])
- Paris Berlin Rome
- 0 1 2
- ["Paris", "Berlin", "Rome"]
- Error
Answer: a) Paris Berlin Rome
Explanation: The loop iterates by index and prints each element in the list.
Question 18: A for loop can be initialized to iterate directly over the elements of a list without needing to use range().
- True
- False
Answer: a) True
Explanation: A for loop can iterate directly over the elements of a list using for element in list:.
Question 19: Using enumerate() is the only way to get both index and value when iterating through a list.
- True
- False
Answer: b) False
Explanation: While enumerate() is a common way to get both index and value, you can also use range(len(list)) with indexing.
Question 20: What happens if you initialize a loop to iterate over an empty list?
- It raises an error
- The loop does nothing
- The loop runs indefinitely
- The list is cleared
Answer: b) The loop does nothing
Explanation: Iterating over an empty list results in no iterations and no output.
Question 21: Which of the following statements about initializing loops for lists is correct?
- A for loop must always use range().
- A while loop is better suited for iterating over lists.
- You can iterate over lists directly or by index using a for loop.
- for loops can only be used with strings.
Answer: c) You can iterate over lists directly or by index using a for loop.
Explanation: for loops can be used to iterate directly over elements or by using indices.
Question 22: Which of the following approaches would correctly initialize a loop that iterates through a list in reverse?
- for i in range(len(my_list), -1, -1):
- for i in range(len(my_list) - 1, -1, -1):
- for element in reversed(my_list):
- for element in my_list[::-1]:
Answer: b) for i in range(len(my_list) - 1, -1, -1):, c) for element in reversed(my_list):, d) for element in my_list[::-1]:
Explanation: All of these methods correctly reverse the iteration order. Option a) is incorrect due to the starting index.
Question 23: The for loop is commonly used to __________ through all elements in a list.
▼Answer: iterate
Explanation: The primary function of a for loop is to iterate through all elements in a list.
Question 24: The enumerate() function can be used to get both the __________ and value when iterating through a list.
▼Answer: index
Explanation: enumerate() returns both the index and the value during iteration.
Question 25: What is the output of the following code?
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit.upper())
- APPLE BANANA CHERRY
- apple banana cherry
- Apple Banana Cherry
- Error
Answer: a) APPLE BANANA CHERRY
Explanation: The loop converts each element to uppercase using .upper() and then prints it.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics