Question 355 of 498
PCEP Control Flow, Loops, Lists and Logic Practice Question
Exhibit
>>> nums = [0, 1, 2, 3, 4, 5] >>> for i in range(len(nums)): ... if nums[i] % 2 == 0: ... nums.remove(nums[i]) ... >>> nums [1, 3, 5]
Refer to the exhibit. Why does the code successfully remove all even numbers but the output is [1, 3, 5]?
⚠ Common exam trap
The PCEP exam often tests the subtle bug of modifying a list while iterating forward, where candidates mistakenly believe the code works correctly because the output happens to be correct in this specific case, overlooking that the logic is flawed and would fail with a different arrangement of numbers.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Because removing elements while iterating forward causes skipping of elements, but in this case the even numbers are at indices 0,2,4 and the removal shifts subsequent elements; however, the loop variable i increments and skips the next odd number, leading to correct removal by chance.
When iterating forward over a list while removing elements, the removal shifts subsequent elements to lower indices, causing the loop variable i to skip the next element. In this specific case, the even numbers happen to be at indices 0, 2, and 4, and after each removal the next element (which is odd) shifts into the current index, but i increments past it, so the odd numbers are never checked and remain, resulting in the correct output by coincidence rather than by correct logic.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Because the condition checks for even numbers and removes them correctly without side effects.
Why it's wrong here
Modifying a list while iterating forward generally causes side effects.
- ✓
Because removing elements while iterating forward causes skipping of elements, but in this case the even numbers are at indices 0,2,4 and the removal shifts subsequent elements; however, the loop variable i increments and skips the next odd number, leading to correct removal by chance.
Why this is correct
The code works because the even numbers are at even indices and after removal, the next element becomes at the same index, but since i increments, it skips the next element; however, the skipped elements are odd and remain, so the result is all odds.
- ✗
Because the range is computed once and the list is shortened, so the loop ends early.
Why it's wrong here
The loop goes through all indices initially, but removal reduces list length.
- ✗
Because the remove() method removes only the first occurrence, and the loop iterates backwards.
Why it's wrong here
The loop does not iterate backwards.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jul 4, 2026
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.