PCEP Control Flow, Loops, Lists and Logic Practice Question
A programmer is building a simple inventory tracker using a Python list named stock that contains integer quantities. They need to add 10 to every quantity in the list, modifying the list in place. Which code correctly performs this operation?
⚠ Common exam trap
The trap here is assuming that modifying the loop variable modifies the list, when actually it only rebinds the variable to a new value.
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(len(stock)): stock[i] = stock[i] + 10
To modify a list in place, you must assign to individual elements using their indices. Using range(len(stock)) provides those indices, and stock[i] = stock[i] + 10 updates each element. The other options either leave the list unchanged, create a new list, or cause an error, so they do not satisfy the in-place requirement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
stock = [qty + 10 for qty in stock]
Why it's wrong here
This list comprehension creates a new list and rebinds the name stock to it, rather than modifying the original list in place. If other references to the original list exist, they will still see the old values. The requirement is to modify the list in place, so this approach does not meet the specification.
- ✗
stock = stock + 10
Why it's wrong here
Adding an integer to a list with the + operator is not valid in Python and raises a TypeError. The + operator concatenates lists, but only with another list. This code would fail at runtime, so it cannot be used to update quantities. It is a common mistake when trying to apply arithmetic to all elements.
- ✗
for qty in stock: qty = qty + 10
Why it's wrong here
This loop assigns a new value to the loop variable qty, but that variable is a separate name bound to each element's value. Reassigning qty does not affect the list because integers are immutable and the loop variable is not a reference to the list slot. The list stock remains unchanged after this loop.
- ✓
for i in range(len(stock)): stock[i] = stock[i] + 10
Why this is correct
Iterating over indices with range(len(stock)) allows direct assignment to each element via stock[i], which modifies the list in place. This is the standard pattern for updating every element when you need to change the original list. The other approaches either do not modify the list or produce incorrect results.
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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.