PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer needs to check if all elements in a list of integers are even. Which code correctly implements this?
⚠ Common exam trap
Python Institute often tests the misconception that `all()` with a condition like `num % 2` checks for even numbers, when in fact it checks for truthy remainders (odd numbers), leading candidates to incorrectly select Option A.
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
✓
all_even = True for num in mylist: if num % 2 != 0: all_even = False break
It initializes `all_even` to `True`, then iterates through the list. If any element is odd (`num % 2 != 0`), it sets `all_even` to `False` and breaks out of the loop early, which is an efficient and correct way to check that all elements are even.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
all_even = all(num % 2 for num in mylist)
Why it's wrong here
all() checks truthiness; odd numbers are truthy, so returns True if all are odd, not even.
- ✗
all_even = any(num % 2 == 0 for num in mylist)
Why it's wrong here
any() returns True if at least one even, not all.
- ✓
all_even = True for num in mylist: if num % 2 != 0: all_even = False break
Why this is correct
Correctly breaks on first odd.
- ✗
all_even = False for num in mylist: if num % 2 == 0: all_even = True else: all_even = False
Why it's wrong here
Flags toggles; final value depends on last element.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way 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. Refer to the exhibit. What is the output?
easy- A.[2, 5, 7, 9, 5]
- B.[2, 4, 6, 8, 10]
- ✓ C.[1, 4, 3, 8, 5]
- D.[1, 2, 3, 4, 5]
Why C: The code iterates over the list `[1, 2, 3, 4, 5]` using indices. It only modifies elements at odd indices (index 1 and 3). For those elements, if the value is even, it multiplies by 2; if odd, it adds 2. At index 1, value 2 (even) becomes 4; at index 3, value 4 (even) becomes 8. Elements at even indices (0, 2, 4) remain unchanged: 1, 3, 5. Thus the final list is `[1, 4, 3, 8, 5]`.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.