PCEP Certification Practice Test: Iterating Through Lists with the for Loop
Here are 25 questions focusing on the topic "iterating through lists with the for loop" for the PCEP-30-02 certification exam. The questions are presented in various formats, including single-select, multiple-select, gap fill, code insertion, sort, and "rearrange" questions. Each question includes the correct answer and an explanation.
Question 1: Which of the following is the correct syntax to iterate through a list named my_list using a for loop?
- for x in range(len(my_list))
- for x in my_list
- for x in list
- for x in range(my_list)
Answer: b) for x in my_list
Explanation: Option b) is the correct way to directly iterate over elements in a list using a for loop.
Question 2: What will be the output of the following code?
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
- ["apple", "banana", "cherry"]
- apple banana cherry
- apple, banana, cherry
- fruit
Answer: b) apple banana cherry
Explanation: The for loop iterates over each element in the list and prints it on a new line.
Question 3: Which of the following code snippets correctly iterates through a list by index?
- for i in range(list)
- for i in range(my_list): print(i)
- for i in range(len(my_list)): print(my_list[i])
- for i in range(len(my_list)): print(i)
Answer: c) for i in range(len(my_list)): print(my_list[i])
Explanation: Option c) uses range(len(my_list)) to iterate over the indices and access each element in the list.
Question 4: Which of the following are valid ways to iterate over a list using a for loop? (Choose all that apply)
- for x in my_list:
- for x in range(len(my_list)):
- for x in enumerate(my_list):
- for i in range(len(my_list)):
Answer: a) for x in my_list:, d) for i in range(len(my_list)):
Explanation: Option a) iterates directly over the elements, and option d) iterates over the indices, accessing the elements via my_list[i]. Option b) lacks the proper indexing method and option c) would require unpacking.
Question 5: Which of the following approaches can be used to iterate over a list and get both index and value? (Choose two)
- for i in range(len(my_list)): print(i, my_list[i])
- for i, value in enumerate(my_list): print(i, value)
- for value in my_list: print(value, i)
- for i, x in range(len(my_list)): print(i, x)
Answer: a) for i in range(len(my_list)): print(i, my_list[i]), b) for i, value in enumerate(my_list): print(i, value)
Explanation: Option a) and b) both correctly retrieve both the index and value during iteration. Option c) is incorrect because i is not defined. Option d) has incorrect syntax.
Question 6: The loop for __________ in my_list: is used to iterate through each element of my_list.
▼Answer: element
Explanation: The loop variable (element) takes the value of each element in my_list during iteration.
Question 7: The loop for __________ in range(len(my_list)): is commonly used to iterate through a list by index.
▼Answer: i
Explanation: The loop variable i is typically used as an index while iterating through a list with range(len(my_list)).
Question 8: Arrange the following code statements in the correct order to iterate over a list and print each element:
- for item in my_list:
- print(item)
- my_list = [1, 2, 3, 4]
Answer: c) my_list = [1, 2, 3, 4]
a) for item in my_list:
b) print(item)
Explanation: The list is defined first, then the for loop is used to iterate over the elements, and each element is printed.
Question 9: Arrange the following steps to iterate over a list by index and print the elements:
- 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: First, the list is defined, then a loop is used to iterate through the list by index, and finally, the elements are printed.
Question 10: Complete the code to print each element of the list names:
names = ["John", "Jane", "Doe"] for __________: print(name)▼
Answer: name in names
Explanation: The correct loop is for name in names: to iterate over each element in the list and print it.
Question 11: Complete the code to iterate through a list by index and print each element in reverse order:
items = [1, 2, 3, 4] for __________: print(items[i])▼
Answer: in range(len(items)-1, -1, -1)
Explanation: The loop for i in range(len(items)-1, -1, -1) iterates through the list indices in reverse order.
Question 12: Insert the correct code to print the index and value of each element in the list colors:
colors = ["red", "green", "blue"] __________ print(index, value)▼
Answer: for index, value in enumerate(colors):
Explanation: The enumerate() function allows you to loop over the list and retrieve both index and value simultaneously.
Question 13: Insert the correct code to iterate over the list numbers and print only even numbers:
numbers = [1, 2, 3, 4, 5, 6] __________ if num % 2 == 0: print(num)▼
Answer: for num in numbers:
Explanation: The loop for num in numbers: iterates over the list and the if condition checks for even numbers.
Question 14: 14. Rearrange the code to iterate over a list and print each element:
- my_list = [10, 20, 30]
- print(item)
- for item in my_list:
Answer: a)my_list = [10, 20, 30]
c) for item in my_list:
b) 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 iterate through a list of names and print each one:
- for name in names:
- print(name)
- names = ["Alice", "Bob", "Charlie"]
Answer: c) names = ["Alice", "Bob", "Charlie"]
a) for name in names:
b) print(name)
Explanation: The list is created first, followed by the loop to iterate through it, and each element is printed.
Question 16: What does the following code output?
nums = [5, 10, 15] for num in nums: print(num * 2)
- 5 10 15
- 10 20 30
- 25 50 75
- Error
Answer: b) 10 20 30
Explanation: The loop multiplies each element by 2 and prints the result.
Question 17: What does the following code output?
names = ["Anna", "Bob", "Clara"] for i in range(len(names)): print(names[i])
- Anna Bob Clara
- 0 1 2
- ["Anna", "Bob", "Clara"]
- Error
Answer: a) Anna Bob Clara
Explanation: The loop iterates by index and prints each element in the list.
Question 18: Using a for loop to iterate over a list allows you to access each element directly.
- True
- False
Answer: a) True
Explanation: The loop for element in list: directly accesses each element during iteration.
Question 19: You can only iterate over lists by their indices using a for loop.
- True
- False
Answer: b) False
Explanation: You can iterate directly over elements without needing to use indices.
Question 20: What happens if you iterate over an empty list using a for loop?
- It raises an error
- The loop runs indefinitely
- The loop does nothing
- The list is cleared
Answer: c) 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 for loops is correct?
- A for loop can only be used with lists.
- A for loop must always use range().
- You can iterate through any iterable using a for loop.
- for loops do not work with strings.
Answer: c) You can iterate through any iterable using a for loop.
Explanation: for loops work with any iterable, including lists, tuples, strings, and more.
Question 22: What will happen if you modify a list while iterating over it using a for loop?
- The loop will skip some elements
- The loop will raise an error
- The loop will continue normally
- The loop will restart from the beginning
Answer: a) The loop will skip some elements
Explanation: Modifying a list while iterating over it can lead to unexpected behavior, like skipping elements.
Question 23: Fill in the blank: The for loop is commonly used to iterate through __________ in a list.
▼Answer: elements
Explanation: The for loop iterates through elements in a list.
Question 24: The function enumerate() can be used to retrieve both __________ and values when iterating through a list.
▼Answer: indices
Explanation: enumerate() returns both the index and the value during iteration.
Question 25: What is the output of the following code?
animals = ["dog", "cat", "bird"] for animal in animals: print(animal.upper())
- dog cat bird
- Dog Cat Bird
- DOG CAT BIRD
- Error
Answer: c) DOG CAT BIRD
Explanation: The loop converts each element to uppercase using .upper() and then prints it.
Test your Python skills with w3resource's quiz
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/data-collections-lists-iterating-through-lists-with-the-for-loop.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics