Questions: Immutability and Mutation

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

In Python, the following code runs: name = 'Alice'; greeting = name; name = name.upper(). What is the value of greeting after these three lines?

A'ALICE' — greeting follows name because they reference the same object
B'Alice' — strings are immutable, so name.upper() created a new string and name was reassigned to it; greeting still points to the original
CNone — the original string was garbage collected when name was reassigned
D'alice' — upper() modifies the string in place and greeting reflects the change
Question 2 Multiple Choice

Two functions both hold a reference to the same list scores = [85, 90, 72]. Function A passes scores to function B. Function B sorts the list in place using scores.sort(). After B returns, what does function A's reference to scores contain?

A[85, 90, 72] — function B received a copy of the list, so the original is unchanged
B[72, 85, 90] — both references point to the same mutable object, so the sort is visible everywhere
CAn error — you cannot sort a list that was passed as a parameter
D[85, 90, 72] — sort() on a function parameter creates a local sorted copy
Question 3 True / False

When a programming language says strings are immutable, it means you can seldom reassign a variable that holds a string to a different string.

TTrue
FFalse
Question 4 True / False

If a mutable list is passed to a function and that function modifies it in place, the caller's variable reflects the change without the function needing to return anything.

TTrue
FFalse
Question 5 Short Answer

What is the difference between reassigning a variable and mutating the object it points to? Why does this distinction matter for reasoning about program correctness?

Think about your answer, then reveal below.