MCQ Hive stays free thanks to ads. Please disable your ad blocker for this site so content and features keep working.
Fundamental Programming
Functions & Parameter Passing
Beginner
Q
What does this Python code print?
def f(L):
L += [4]
a = [1]
f(a)
print(a)
A[1, 4]
B[4]
C[1]
DUnboundLocalError
Correct Answer:
A. [1, 4]
Explanation:
For lists, += calls __iadd__, which extends the same object in place, so the change is visible to the caller.
ALSO KNOW (related points):
• What does this Python code print?: def f(L):; L = L + [4]; a = [1]; f(a); print(a) → [1]
• In def f(*args):, what is the type of args? → tuple
• Given def f(a, *, b):, which call is valid? → f(1, b=2)