PCEP Control Flow, Loops, Lists and Logic Practice Question
Exhibit
numbers = [5, 2, 8, 1, 9]
for i in range(len(numbers)):
for j in range(0, len(numbers)-i-1):
if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
print(numbers[0])Refer to the exhibit. What is the output?
⚠ Common exam trap
Candidates might mistakenly think that the break occurs before the assignment of x, so x remains 0. However, the code assigns x = i before checking the condition, so x becomes 1 before the loop breaks.
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
✓
1
(1). The code initializes x to 0, then iterates over the list [1, 2, 3, 4, 5]. In each iteration, it first assigns the current number to x, then checks if the number equals 1. On the first iteration, i is 1, so x becomes 1, then the condition i == 1 is true, causing the loop to break. The print(x) statement after the loop outputs the final value of x, which is 1.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
5
Why it's wrong here
5 is the middle element.
- ✗
2
Why it's wrong here
2 is the second element after sorting.
- ✗
9
Why it's wrong here
9 is the largest, at the end.
- ✓
1
Why this is correct
After bubble sort, the smallest element is at index 0.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 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 →
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 of the code?
medium- A.0 1 2 4 5
- ✓ B.1 2 4 5
- C.1 2 3 4 5
- D.2 4 5
Why B: The code uses a for loop with range(1,6) to iterate over numbers 1 through 5. Inside the loop, an if statement checks if the current number equals 3; if true, the continue statement skips the print(i) for that iteration. Thus, when i is 3, the print is skipped, so the output is '1 2 4 5'. Option B is correct because it matches this output.
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.