ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def f(x, lst=[]):
lst.append(x)
return lst
a = f(1)
b = f(2, [])
c = f(3)
print(a, b, c)- A. [1] [2] [1, 3]
- B. [1, 3] [2] [3]
- C. [1] [2] [3]
- D. [1, 3] [2] [1, 3]
Correct answer: D. [1, 3] [2] [1, 3]
a and c are the SAME default list object, which holds [1, 3] once both calls have run. b got its own new list [2]. The print happens after all three calls, so a also shows [1, 3].
More Python Programming questions
- What is printed? python def f(): try: return 1 finally: return 2 print(f())
- What is printed? python print(1 < 2 > 1.5, 3 > 2 == 2, (3 > 2) == 2)
- What is printed? python a = [1, 2, 3] b = a a = a + [4] b += [5] print(a, b)
- What is printed? python print(-2 2, (-2) 2, 2 -1)
- What is printed? python print(0 or [] or "x", 1 and 0 and 5, None or 0)
- What is printed? python x = [1, 2] g = (n 10 for n in x) x.append(3) x = [7] print(list(g))