PCEP Control Flow, Loops, Lists and Logic Practice Question
A program contains a nested while loop. The inner loop should run as long as a condition is True, but the outer loop should stop after 3 iterations. Which code structure is correct? (Assume the inner loop condition is inner < 5.)
⚠ Common exam trap
Python Institute often tests the misconception that incrementing a loop counter inside a nested loop will correctly control both loops, when in fact it causes the outer loop to terminate prematurely, as seen in options B and C.
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
✓
outer = 0 while outer < 3: inner = 0 while inner < 5: # do something inner += 1 outer += 1
Ly implements a nested while loop where the inner loop runs while `inner < 5` and the outer loop runs while `outer < 3`. The inner loop increments `inner` to control its own termination, and the outer loop increments `outer` after the inner loop completes, ensuring exactly 3 iterations of the outer loop. This matches the requirement that the outer loop stops after 3 iterations while the inner loop runs as long as its condition is True.
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 outer in range(3): inner = 0 while inner < 5: # do something inner += 1
Why it's wrong here
Uses for loop for outer, not while as required.
- ✗
outer = 0 while outer < 3: for inner in range(5): # do something outer += 1
Why it's wrong here
Increments outer in inner loop; outer reaches 3 quickly.
- ✗
outer = 0 while outer < 3: inner = 0 while inner < 5: # do something outer += 1 inner += 1
Why it's wrong here
Increments outer in inner loop; outer reaches 3 prematurely.
- ✓
outer = 0 while outer < 3: inner = 0 while inner < 5: # do something inner += 1 outer += 1
Why this is correct
Correct; outer increments after inner loop completes.
Go deeper
Related to this question
About these practice questions
One of 498 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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. Consider code: def outer(): x = 1 def inner(): nonlocal x x = 2 inner() print(x) outer() What is printed?
hard- ✓ A.2
- B.1
- C.Error
- D.None
Why A: The `nonlocal` declaration inside `inner()` binds the variable `x` to the `x` defined in the enclosing `outer()` function. When `inner()` assigns `x = 2`, it modifies that outer `x`, so after `inner()` returns, `print(x)` in `outer()` outputs 2.
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.