Functions & Scope · Question 20 · Hard
Inside update, one list is changed with append and the other name is reassigned with +. What do x and y look like after the call?
def update(a, b):
a.append(4)
b = b + [4]
x = [1]
y = [1]
update(x, y)
print(x, y)
- [1, 4] [1, 4]
- [1] [1]
- [1, 4] [1]
- [1] [1, 4]
Answer and explanation
Answer: C. [1, 4] [1]
Explanation: a.append(4) changes the same list object that x refers to, so x becomes [1, 4]. b = b + [4] builds a new list and binds only the local name b to it, so y is still [1].
Previous: Which statement about the nonlocal keyword in Python is correct?
Practise functions & scope
Try a fresh set of functions & scope questions with an explanation after every answer, free and without signing up: Functions & Scope practice. Or sit a full placement mock test.
More functions & scope questions
- A default parameter value refers to a module-level variable that is changed…
- The recursive function below counts how many moves a number needs to reach 1…
- Which statement about the nonlocal keyword in Python is correct?
More python practice
Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge