AP CSP Day 29: List Mutation & Aliasing
Share
Big Idea 3
Day 29 Practice
Focus: List Mutation & Aliasing
Practice Question
Consider the following code segment:
listA ← [10, 20, 30]
listB ← listA
listB[2] ← 99
DISPLAY(listA[2])Why This Answer?
When listB is assigned to listA, both variables reference the SAME list in memory (aliasing). Modifying listB[2] changes the shared list, so listA[2] also becomes 99.
Why Not the Others?
A) 10 is listA[1], not listA[2].
B) 20 is the original but the list was modified.
C) 30 was original before modification through listB.
Common Mistake
Watch Out!
Assuming listB gets a copy. List assignment creates an alias (reference), not a copy.
AP Exam Tip
Watch for list assignments - they create references. Changes through one variable affect all aliases.