ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
print(sum([0.1] * 10) == 1.0, sum([0.1] * 10), sum([0.1] * 10, 0) == 1.0)
- A. False 0.9999999999999999 True
- B. True 1.0 True
- C. True 1.0 False
- D. False 0.9999999999999999 False
Correct answer: B. True 1.0 True
Since Python 3.12, sum() of floats uses compensated (Neumaier) summation, so the rounding error is recovered and the result is exactly 1.0. A plain for-loop with += still gives 0.9999999999999999.
More Python Programming questions
- What is printed? python print(int(3.9999999999999999), int(-3.7), round(-3.7))
- What is printed? python print(hash(1) == hash(1.0), hash(True) == 1, {1: 'a', 1.0: 'b', True: 'c'})
- What is printed? python print(0.1 3 == 0.3, 0.1 3, 0.3 3 == 0.9)
- What is printed? python inf = float('inf') print(inf > 10400, inf - inf == 0, -inf < -10400, inf == inf)
- What is printed? python print(len(str(21000)), sum(int(d) for d in str(2100)))
- What is printed? python import math print(math.gcd(0, -6), math.lcm(4, 6), -7 % -3, math.lcm(0, 5))