In Python, `def f(lst): lst.append(99)` is called with `mylist = [1, 2, 3]; f(mylist)`. What does `mylist` contain after the call?
A[1, 2, 3] — Python uses pass-by-value, so the function received a copy of the list
B[1, 2, 3, 99] — the function mutated the shared object through the copied reference
CIt depends on whether mylist was declared global inside the function
D[99] — appending inside a function replaces the original list
Python passes a copy of the *reference* (pointer) to the list, not a copy of the list itself. Both `lst` inside the function and `mylist` in the caller point to the same list object in memory. When `lst.append(99)` mutates that object, the change is visible through both references. This is 'pass-by-object-reference': the reference is copied, but the object is shared. Note that option A is wrong because Python does NOT copy the list — only the reference to it is copied.
Question 2 Multiple Choice
In Python, `def g(lst): lst = [10, 20, 30]` is called with `mylist = [1, 2, 3]; g(mylist)`. What does `mylist` contain after the call?
A[10, 20, 30] — reassignment inside the function replaces the caller's list
B[1, 2, 3] — reassignment rebinds only the local parameter; the caller's reference is unaffected
C[] — reassignment always clears the original list before binding to the new one
D[1, 2, 3, 10, 20, 30] — reassignment concatenates to the original list
This is the essential contrast with the previous scenario. When the function executes `lst = [10, 20, 30]`, it rebinds the local parameter `lst` to point at a new list object. This only changes where the function's local copy of the reference points — the caller's `mylist` variable still points at the original list [1, 2, 3], which is unchanged. The key distinction: mutating an object (append, in-place modification) affects the caller; reassigning the parameter does not.
Question 3 True / False
In pass-by-value, modifying the parameter variable inside the function changes the original variable in the caller.
TTrue
FFalse
Answer: False
Pass-by-value means the function receives an independent copy of the argument's value. The parameter and the caller's variable are completely separate — they happen to start with the same value, but modifications to one do not affect the other. This is like photocopying a document: writing on the copy leaves the original untouched. In C, all primitive parameters are passed by value. In Python, immutable objects like integers behave analogously — you cannot change a 5 to a 6 by modifying the function's copy.
Question 4 True / False
In Python and Java, when an object is passed to a function, the reference to that object is passed by value — meaning the function can mutate the object's contents but cannot redirect the caller's variable to a different object.
TTrue
FFalse
Answer: True
This is the defining characteristic of 'pass-by-object-reference' (also called 'pass-by-sharing'). The function receives a copy of the pointer/reference to the object. Because both the caller and the function hold references to the same object, mutations through either reference are visible to both. But if the function rebinds its local parameter to point at a new object, it is only changing its own copy of the reference — the caller's variable still points at the original. This explains why `lst.append(1)` modifies the caller's list but `lst = []` does not.
Question 5 Short Answer
Explain the difference between mutating an object and reassigning a parameter in Python. Why does one affect the caller but not the other?
Think about your answer, then reveal below.
Model answer: In Python, a function parameter receives a copy of the reference (pointer) to the object, not a copy of the object itself. If the function mutates the object through that reference — by calling a method like append() or modifying an attribute — both the function and the caller see the change, because they share the same underlying object. But if the function reassigns the parameter to a new object (e.g., lst = []), it only redirects its own local copy of the reference. The caller's variable still points at the original object, which is unmodified. The rule: shared object, copied reference.
This distinction is the source of most parameter-passing confusion in Python. The mental model to internalize: Python always passes the reference by value. 'By value' means the reference copy is independent of the caller's variable — reassignment doesn't propagate back. 'Shared object' means mutations through the reference do propagate back, because both sides are looking at the same memory location.