PCEP-30-02 · Question #15
Take a look at the snippet and choose one of the following statements which is true: ``python 1 nums = [] 2 vals = nums 3 vals.append(1) ``
The correct answer is B. nums and vals are of the same length. Option B is correct because vals = nums does not create a new list - it creates a second reference pointing to the same list object in memory, so any mutation through either name (like vals.append(1)) is immediately visible through both. After line 3, both nums and vals refer…
Question
1 nums = []
2 vals = nums
3 vals.append(1)
Options
- Avals is longer than nums
- Bnums and vals are of the same length
- Cnums is longer than vals
How the community answered
(35 responses)- A9% (3)
- B71% (25)
- C20% (7)
Explanation
Option B is correct because vals = nums does not create a new list - it creates a second reference pointing to the same list object in memory, so any mutation through either name (like vals.append(1)) is immediately visible through both. After line 3, both nums and vals refer to the list [1], making their lengths identical (1). Options A and C are wrong for the same reason: since there is only one list object, one variable cannot be longer than the other - the distinction implied by "longer" assumes two separate lists, which never exist here. Memory tip: Think of vals = nums like writing two names on a sticky note that points to one box - adding something to the box affects both names simultaneously.
Community Discussion
No community discussion yet for this question.