PCEP-30-02 · Question #165
If a list passed into a function as an argument, deleting any of its elements inside the function using the del instruction:
The correct answer is C. will affect the argument. In Python, lists are passed by reference (technically "pass-by-object-reference"), meaning the function receives a reference to the same list object in memory - so any del operation inside the function directly modifies the original list, making C correct. Option A is wrong…
Question
Options
- Awill not affect the argument
- Bwill cause a runtime error
- Cwill affect the argument
How the community answered
(46 responses)- A17% (8)
- B11% (5)
- C72% (33)
Explanation
In Python, lists are passed by reference (technically "pass-by-object-reference"), meaning the function receives a reference to the same list object in memory - so any del operation inside the function directly modifies the original list, making C correct. Option A is wrong because mutations to a mutable object (like a list) are visible outside the function; only reassigning the variable entirely (arg = []) would leave the original untouched. Option B is wrong because deleting a valid list element with del is perfectly legal and raises no runtime error. Memory tip: Think of a list as a shared document - passing it to a function hands someone the same file, not a copy, so deletions stick.
Community Discussion
No community discussion yet for this question.