ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
from functools import reduce print(reduce(lambda a, b: a * 10 + b, [1, 2, 3]), reduce(lambda a, b: a - b, [10, 2, 3]), reduce(lambda a, b: a - b, [], 0))
- A. 123 11 0
- B. 321 5 0
- C. 123 5 0
- D. 123 5 None
Correct answer: C. 123 5 0
reduce folds from the left, so 10 - 2 - 3 is 5 and not 10 - (2 - 3). With an empty list and an initial value of 0, reduce returns that initial value.
More Python Programming questions
- What is printed? python print(sum(1 for c in 'a1b22c333' if c.isdigit()), ''.join(c for c in 'a1b22c333' if c.isalpha()), int(''.join(c…
- What is printed? python print(str(0.1 3), repr(0.1 3) == '0.30000000000000004', 0.1 3 == float(repr(0.1 3)))
- What is printed? python print(10-1 + 10-1 + 10-1 == 0.3, 1/10 + 1/10 + 1/10 == 0.3, 10-1 == 1/10)
- What is printed? python print(sorted([3, 1, 2], reverse=True) == [3, 2, 1], [3, 1, 2][::-1], sorted('cab')[::-1])
- What is printed? python print(int(str(7) 3) 2 % 10, 111 2, (11 2) 10 + 1)
- What is printed? python n = float('nan') print(n in [n], float('nan') in [n], n == n, [n] == [n])