w3resource

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?

  1. for x in range(len(my_list))
  2. for x in my_list
  3. for x in list
  4. 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)
  1. ["apple", "banana", "cherry"]
  2. apple banana cherry
  3. apple, banana, cherry
  4. 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?

  1. for i in range(list)
  2. for i in range(my_list): print(i)
  3. for i in range(len(my_list)): print(my_list[i])
  4. 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)

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

  1. for i in range(len(my_list)): print(i, my_list[i])
  2. for i, value in enumerate(my_list): print(i, value)
  3. for value in my_list: print(value, i)
  4. 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:

  1. for item in my_list:
  2. print(item)
  3. 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:

  1. for i in range(len(my_list)):
  2. print(my_list[i])
  3. 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:

  1. my_list = [10, 20, 30]
  2. print(item)
  3. 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:

  1. for name in names:
  2. print(name)
  3. 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)
  1. 5 10 15
  2. 10 20 30
  3. 25 50 75
  4. 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])
  1. Anna Bob Clara
  2. 0 1 2
  3. ["Anna", "Bob", "Clara"]
  4. 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.

  1. True
  2. 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.

  1. True
  2. 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?

  1. It raises an error
  2. The loop runs indefinitely
  3. The loop does nothing
  4. 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?

  1. A for loop can only be used with lists.
  2. A for loop must always use range().
  3. You can iterate through any iterable using a for loop.
  4. 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?

  1. The loop will skip some elements
  2. The loop will raise an error
  3. The loop will continue normally
  4. 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())
  1. dog cat bird
  2. Dog Cat Bird
  3. DOG CAT BIRD
  4. 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



Become a Patron!

Follow us on Facebook and Twitter for latest update.