Questions: Iterating Over Collections

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

You have a list [1, 2, 3, 4, 5] and write a loop to remove all even numbers by calling remove() on each even element directly during the iteration. What is the most likely outcome?

AAll even numbers are correctly removed, leaving [1, 3, 5]
BThe loop crashes immediately with an index error on the first removal
CSome even numbers are silently skipped, producing incorrect output
DThe list is unchanged because remove() is not allowed inside loops
Question 2 Multiple Choice

You need to process every element in a list and also need to know the index of each element. Which approach is most idiomatic in Python?

AA while loop with a manually managed counter variable
BA plain for-each loop (for item in items) and track the index separately
Cenumerate() to get both index and value in each iteration
DAn index-based loop using range(len(items)) and items[i]
Question 3 True / False

A for-each loop (e.g., 'for item in items') eliminates off-by-one errors because you never manage an index variable manually.

TTrue
FFalse
Question 4 True / False

It is safe to remove elements from a list while iterating over it with an index-based for loop, because the index variable generally tracks your current position correctly.

TTrue
FFalse
Question 5 Short Answer

Why is it dangerous to modify a collection while iterating over it, and what is the standard safe alternative?

Think about your answer, then reveal below.