PCEP Control Flow, Loops, Lists and Logic Practice Question
Which TWO of the following code snippets will produce the output '0 1 2'? (Choose two.)
⚠ Common exam trap
Python Institute often tests the distinction between a for loop with range() and a while loop that requires an explicit increment; the trap here is that Option E looks like it should work but omits the increment, leading to an infinite loop, which candidates may overlook if they mentally add the missing i+=1.
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
✓
for i in range(5): if i>2: break; print(i)
The loop iterates over range(5) (0,1,2,3,4), but the break statement executes when i > 2, so the loop terminates after printing 0, 1, and 2. Option D is correct because range(3) generates exactly the sequence 0, 1, 2, and each value is printed in order.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for i in range(5): if i%2==0: print(i); else: continue
Why it's wrong here
Prints all even numbers: 0,2,4.
- ✗
i=0; while i<3: print(i); i+=1; else: print('done')
Why it's wrong here
Prints 0,1,2,done (extra 'done').
- ✓
for i in range(5): if i>2: break; print(i)
Why this is correct
Correct: prints 0,1,2 then breaks.
- ✓
for i in range(3): print(i)
Why this is correct
Correct: prints 0,1,2.
- ✗
i=0; while i<3: print(i)
Why it's wrong here
Missing increment; infinite loop printing 0 repeatedly.
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. Order the steps to write a for loop that iterates over a range of numbers.
medium- ✓ A.Start with 'for', then variable name, then 'in', then range() with start, stop, step, then colon, then indented loop body
- B.Start with range(), then 'for', then variable name, then 'in', then colon, then indented loop body
- C.Start with 'for', then variable name, then colon, then 'in', then range(), then indented loop body
- D.Start with 'for', then 'in', then variable name, then range(), then colon, then indented loop body
Why A: The correct order is to write 'for', then the loop variable, then 'in', then the range() function with appropriate arguments, then a colon, and finally an indented block containing the statements to repeat. This structure defines a for loop that iterates over a sequence generated by range().
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.