PCEP Control Flow, Loops, Lists and Logic Practice Question
Exhibit
data = [1, 2, 3, 4, 5]
result = []
for x in data:
if x % 2 == 0:
result.append(x * 2)
else:
result.append(x * 3)
print(result[2])What is the output of the following code?
numbers = [1, 2, 3, 4, 5] total = 0
for i in numbers:
if i % 2 == 0:continue total += i
print(total)
⚠ Common exam trap
Python Institute often tests the `continue` statement by embedding it inside a conditional that filters out specific values, leading candidates to mistakenly sum all elements or incorrectly include the skipped values.
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
✓
9
The exhibit shows code that iterates over the list [1, 2, 3, 4, 5] and uses `if i % 2 == 0: continue` to skip even numbers. Only odd numbers (1, 3, 5) are summed, giving 1 + 3 + 5 = 9. Therefore, option D is correct.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
10
Why it's wrong here
Skipping even values via continue sums only the odd numbers 1, 3 and 5, giving 9, not 10. The figure 10 tempts because it is the sum of all five list elements, which would be the output only if the conditional continue were removed entirely.
- ✗
8
Why it's wrong here
The loop skips even values and adds only odd ones: 1+3+5=9, not 8. Summing 1+3+5 gives 9; 8 would result only if the loop added 3+5 or otherwise omitted an odd number from the total.
- ✗
6
Why it's wrong here
Odd values 1, 3 and 5 sum to 9, not 6. The continue statement skips evens, so all three odd numbers accumulate; 6 would require excluding one of them, such as 1, from the running total.
- ✓
9
Why this is correct
The loop skips even values via continue, so only odd numbers accumulate: 1 + 3 + 5 = 9. Even numbers 2 and 4 are excluded before total += i executes, and the final print outputs 9.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-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. A developer is writing a function that takes a list of numbers and returns the sum of all even numbers. Which two code snippets correctly implement this function? (Select two.)
medium- A.def sum_even(nums): return [n for n in nums if n%2==0]
- B.def sum_even(nums): total=0; for i in range(len(nums)): if nums[i]%2==0: total+=nums[i]*2; return total
- ✓ C.def sum_even(nums): total=0; for n in nums: if n%2==0: total+=n; return total
- D.def sum_even(nums): total=0; for n in nums: if n%2==1: total+=n; return total
- ✓ E.def sum_even(nums): return sum(n for n in nums if n%2==0)
Why C: It initializes a total variable to 0, iterates over each number in the list, checks if it is even using the modulo operator (n % 2 == 0), and adds the number to the total. This correctly accumulates the sum of all even numbers and returns the final total.
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.