ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
print(sum([0.1] * 3) == 0.3, sum([0.1] * 3, 0.0) == 0.30000000000000004, sum([1e100, 1.0, -1e100]))
- A. True True 1.0
- B. False True 0.0
- C. False True 1.0
- D. False False 1.0
Correct answer: C. False True 1.0
Adding three 0.1 values gives 0.30000000000000004 rather than the double for 0.3. Since Python 3.12, sum() uses compensated summation, which recovers the 1.0 that a plain loop would lose.
More Python Programming questions
- What is printed? python print(-(2 3) 2, (-2) 3 2, -2 -1, 0)
- What is printed? python print(7 & 3 << 1, 7 & (3 << 1), 6 | 1 << 2, (6 | 1) << 2)
- What is printed? python print(~0, ~-1, -~0, ~~5, -~-5)
- What is printed? python print(max(map(int, '3 17 9'.split())), min(map(int, '3 17 9'.split())), sorted('3 17 9'.split())[-1])
- What is printed? python print(sorted(['1.10', '1.9', '1.10.1'], key=lambda v: tuple(map(int, v.split('.')))))
- What is printed? python print(sorted(['1.10', '1.9', '1.10.1']))