Predict the output
When is the array's storage actually copied?
var a = [1, 2, 3]
var b = a
b.append(4)
print(a.count, b.count)Hint 1 of 1
What triggers the copy?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B At b.append(4)
Correct. The first write to a shared buffer makes b copy it; this prints 3 4.
Why not the others?
- A At
var b = a. Assignment shares the buffer; nothing is copied yet. - C Never, both share one buffer. Then a would see the append; a still has 3 elements.
- D At
print. Reading doesn't copy.