ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def f(x, acc=[]):
acc.append(x)
return acc
f(1)
f(2)
print(f(3, []), f(4))- A. [3] [4]
- B. [3] [1, 2, 4]
- C. [1, 2, 3] [1, 2, 4]
- D. [3] [1, 2]
Correct answer: B. [3] [1, 2, 4]
Passing [] explicitly creates a fresh list, so f(3, []) returns [3]. f(4) uses the default list, which already holds 1 and 2, so it returns [1, 2, 4].
More Python Programming questions
- What is printed? python def gridpaths(r, c, memo={}): if r == 0 or c == 0: return 1 if (r, c) not in memo: memo[(r, c)] = gridpaths(r - 1…
- What is printed? python def most2(s): cnt = {} l = best = 0 for r, ch in enumerate(s): cnt[ch] = cnt.get(ch, 0) + 1 while len(cnt) > 2…
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…
- What is printed? python print(max('bca'), min('bca', key=lambda c: (c, -ord(c))))
- What is printed? python from collections import deque d = deque([1, 2, 3]) d.extendleft([4, 5]) print(list(d))
- What is printed? python x = 'abc' print(x.replace('', '-'))