Question 440 of 498
PCEP Control Flow, Loops, Lists and Logic Practice Question
A program uses a while loop to find the largest number in a list until a negative number is encountered. What is wrong with this code?
numbers = [3, 7, 2, -1, 5] max_num = 0 i = 0
while numbers[i] >= 0:
if numbers[i] > max_num:max_num = numbers[i] i += 1
print(max_num)
⚠ Common exam trap
Python Institute often tests the misconception that a while loop's condition should mirror the data filter (e.g., 'continue while number is non-negative'), when in reality the condition should control the iteration range (e.g., index within bounds) and the filter should be an inner `if` statement.
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
✓
The loop should continue even when number is negative (skip negatives).
The while loop condition `numbers[i] >= 0` causes the loop to terminate as soon as it encounters a negative number (-1), so it never processes the number 5 at the end of the list. The intended behavior is to skip negative numbers and continue iterating through the entire list to find the largest number among all non-negative values. To fix this, the loop should use a condition that checks the index against the list length (e.g., `i < len(numbers)`) and then skip negative numbers with an `if` statement inside the loop.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The loop should start with max_num = None.
Why it's wrong here
While initializing max_num to 0 works if the list contains only non-negative numbers, it is not the main problem. The real issue is the loop condition stops early when encountering a negative number. Therefore, A is incorrect.
- ✗
The loop should check all numbers until end of list.
Why it's wrong here
Suggests checking all numbers until the end of the list. While that would fix the early termination, it does not explicitly address skipping negative numbers. The most precise fix is to continue iteration and skip negatives, as stated in option C. Thus, B is not the best answer.
- ✓
The loop should continue even when number is negative (skip negatives).
Why this is correct
This is correct. The loop condition should not filter out negatives; instead, it should iterate through the entire list using an index check (e.g., i < len(numbers)), and inside the loop, skip negative numbers with an if statement. This ensures all numbers are considered for finding the largest non-negative number.
- ✗
The loop should use a for loop instead.
Why it's wrong here
Using a for loop could work, but the while loop solution with proper condition and inner check is equally valid. The question asks what is wrong with the given code, and the primary issue is the loop condition, not the loop type. Therefore, D is incorrect.
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 →
Same concept, more angles
2 more ways this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A program uses a for loop to double each element in a list: numbers = [1, 2, 3, 4, 5]; for num in numbers: num = num * 2. After execution, numbers remains unchanged. Why?
medium- ✓ A.Reassigning num does not modify the original list element; you need to modify via index.
- B.The assignment creates a new list, leaving the original unchanged.
- C.Lists are immutable; their elements cannot be changed.
- D.The variable num is a copy of the list element.
Why A: In Python, the loop variable `num` is a reference to each element in the list, but reassigning `num` (e.g., `num = num * 2`) merely rebinds the local variable to a new integer object; it does not modify the original list element. To change the list in place, you must access elements by their index, such as `numbers[i] = numbers[i] * 2`.
Variation 2. A developer needs to write a loop that prints all even numbers from a list. They attempt: for num in numbers: if num % 2 == 0: print(num). However, they want a more efficient approach using list comprehension. Which alternative achieves the same result?
medium- A.print([num for num in numbers if num % 2 == 0])
- B.for num in numbers: print(num if num % 2 == 0 else None)
- ✓ C.for even in [num for num in numbers if num % 2 == 0]: print(even)
- D.print([num % 2 == 0 for num in numbers])
Why C: It uses a list comprehension to generate a list of even numbers, then iterates over that list with a for loop, printing each even number. This achieves the same result as the original loop but with the efficiency of list comprehension for filtering, while still printing each number individually.
Last reviewed: Jun 30, 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.