MCQ Hive stays free thanks to ads. Please disable your ad blocker for this site so content and features keep working.
Fundamental Programming
Variables & Scope
Beginner
Q
What does this Python code print?
def f(x, L=[]):
L.append(x)
return L
f(1)
print(f(2))
A[[1], 2]
B[1, 2]
C[1]
D[2]
Correct Answer:
B. [1, 2]
Explanation:
A default argument is evaluated once, when the function is defined, so the same list is shared across calls. A common fix is to use L=None and create the list inside the function.
ALSO KNOW (related points):
β’ What is the value of a global int variable declared without an initializer in C++? β 0
β’ What does this C++ code print?: int x = 1;; { int x = 2; x++; }; cout