PCEP Control Flow, Loops, Lists and Logic Practice Question
Exhibit
>>> my_list = [1, 2, 3, 4, 5] >>> for i in range(len(my_list)): ... if my_list[i] % 2 == 0: ... my_list[i] = my_list[i] ** 2 ... else: ... my_list[i] = my_list[i] ** 3 >>> my_list [1, 4, 27, 16, 125]
Refer to the exhibit. What is the final value of my_list?
⚠ Common exam trap
A common mix-up: candidates assume the exponent is fixed (e.g., 2 or 3) rather than recognizing that the exponent changes with each iteration, leading them to pick the squaring or cubing options.
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, 4, 27, 16, 125]
The code iterates over indices 0 to 4 of my_list, and for each index i, it assigns my_list[i] = (i+1) ** (i+1). This computes 1^1, 2^2, 3^3, 4^4, 5^5, resulting in [1, 4, 27, 16, 125]. Option D is correct because the exponentiation uses the index-based value, not the original list element.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
[1, 2, 3, 4, 5]
Why it's wrong here
The list is modified.
- ✗
[1, 4, 9, 16, 25]
Why it's wrong here
This would be all squares.
- ✗
[1, 8, 27, 64, 125]
Why it's wrong here
This would be all cubes.
- ✓
[1, 4, 27, 16, 125]
Why this is correct
Even numbers (2,4) are squared, odd numbers (1,3,5) are cubed.
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
2 more ways 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. Given the code: my_list = [1, 2, 3, 4, 5]. What is the output of print(my_list[-3:-1])?
hard- A.[4, 5]
- ✓ B.[3, 4]
- C.[2, 3]
- D.[3, 4, 5]
Why B: Python list slicing with negative indices counts from the end of the list. my_list[-3:-1] starts at index -3 (value 3) and goes up to but does not include index -1 (value 5), so it returns [3, 4].
Variation 2. Refer to the exhibit. What is the output?
easy- A.[1, 2, 3]
- B.[0, 1, 2]
- ✓ C.[1, 2]
- D.[2, 3]
Why C: The code creates a list [1,2,3] and uses slicing with start index 0 and end index 2 (exclusive), extracting elements at indices 0 and 1, which are 1 and 2. Therefore, the output is [1,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.