PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer is troubleshooting a loop that terminates earlier than expected. The code is: i = 0; while i < 5: if i == 3: break; print(i); i += 1. What is the output?
⚠ Common exam trap
The trap here is that candidates often mistakenly think the break happens after printing the value that triggers it (i==3), rather than before the print statement in the same iteration.
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
✓
0 1 2
The loop starts with i=0 and increments i by 1 each iteration. When i reaches 3, the break statement executes, immediately terminating the loop. Therefore, only i values 0, 1, and 2 are printed, making option D 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.
- ✗
0 1 2 3 4
Why it's wrong here
The break at i == 3 prevents 3 and 4 from printing, so the loop cannot reach completion. Listing all five values is tempting because the while condition i < 5 would allow them, but the break exits before i ever reaches 4, truncating the sequence at 2.
- ✗
0 1 2 3
Why it's wrong here
The break at i == 3 exits before print(3) runs, so 3 never appears; the loop prints 0, 1, 2. Including 3 is tempting because the condition i == 3 is evaluated, but evaluation is not output — break halts the loop immediately, before that iteration's print statement executes.
- ✗
0 1
Why it's wrong here
The loop does not stop at i == 2; the break triggers only when i equals 3, so 0, 1 and 2 all print first. Stopping after two values is tempting if you misread the condition as i == 2, but the code compares against 3, and i increments after each print.
- ✓
0 1 2
Why this is correct
The loop prints i while i is below 5, but the break statement exits immediately when i equals 3, before that value is printed. Output is therefore 0, 1 and 2, terminating earlier than the expected 0 through 4.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.