ProgrammingVery hardGATE CS · Product-company placement
What is printed (Python 3.12 or later)?
What is printed (Python 3.12 or later)?
from functools import reduce xs = [0.1] * 10 print(sum(xs) == 1.0, reduce(lambda a, b: a + b, xs) == 1.0, sum(xs[:3]) == 0.3)
- A. True False False
- B. False False False
- C. True True True
- D. False False True
Correct answer: A. True False False
Since Python 3.12, sum() of floats uses compensated (Neumaier) summation, so ten 0.1s give exactly 1.0, while a plain left-to-right reduce drifts to 0.9999999999999999. For three 0.1s even the correctly rounded sum is 0.30000000000000004, not 0.3.
More Python Programming questions
- What is printed? python nan = float("nan") print(max(nan, 1), max(1, nan), min(nan, 1), nan in {nan})
- What is printed? python print(max([1, 2], [1, 2, 0]), min("Z", "a", ""), sorted([True, 0, -1, 0.5]), max(3, 3.0), max(3.0, 3))
- What is printed? python print(bin(-5), (-5).bitlength(), ~5, -5 >> 1, -5 & 0b111, 5 ^ -1)
- What is printed? python print("10" + "5", int("10") + 5, "10" 2, str(10) 2 == "1010", "5" < "10")
- What is printed? python a = "hello" try: a[0] = "j" except TypeError: a = "j" + a[1:] b = a.replace("l", "X", 1) print(a, b…
- What is printed? python print(1000 == 1000, 0.1 + 0.7, 7 / 2 == 7 // 2 + 0.5, 2 0.5 2, (-8) (1 / 3) == -2)