ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
ops = {}
for name in ['add', 'mul']:
ops[name] = lambda x, y, n=name: x + y if n == 'add' else x * y
print(ops['add'](2, 3), ops['mul'](2, 3))- A. 5 6
- B. 5 5
- C. 6 6
- D. 5 2
Correct answer: A. 5 6
The default argument n captures each name when its lambda is created, so add and mul pick the right branch. 2 + 3 = 5 and 2 * 3 = 6.
More Python Programming questions
- What is printed? python print(list(enumerate('ab', start=1)), list(zip(reversed([1, 2, 3]), 'xyz')))
- What is printed? python print(hash(1) == hash(True), hash(-1) == -2)
- Which statement about dictionary iteration order in Python 3.7 and later is correct?
- What is printed? python class Q: def init(self): self.i, self.o = [], [] def push(self, x): self.i.append(x) def pop(self): if not self.o…
- What is printed? python print([y for x in [[1, 2], [3], []] for y in x if y % 2])
- What is printed? python out = [] for i in range(3): try: if i == 1: continue out.append(i) finally: out.append('f') print(out)