ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sum([0.1, 0.2, 0.3]) == 0.6, 0.1 + 0.2 + 0.3 == 0.6, 0.3 + 0.2 + 0.1 == 0.6)
- A. True True False
- B. False True False
- C. True False True
- D. False False True
Correct answer: C. True False True
Since Python 3.12, sum() of floats uses compensated summation, so sum([0.1, 0.2, 0.3]) returns exactly 0.6. Plain left-to-right + gives 0.6000000000000001, so 0.1 + 0.2 + 0.3 == 0.6 is False, while adding 0.3 + 0.2 first gives 0.5 and then 0.6 exactly.
More Python Programming questions
- What is printed? python print(7.5 // 2, 7.5 % 2, -7.5 // 2, divmod(7.5, -2))
- What is printed? python print(max([('x', 5), ('y', 5)], key=lambda t: t[1]), min([('x', 5), ('y', 5)], key=lambda t: t[1]))
- What is printed? python print(sorted([1.0, True, 1, 0.5, False]))
- What is printed? python try: print(int('10')) except ValueError: print('bad')
- What is printed? python print(float('1e400'), -float('1e400'), float('1e308') 10)
- What is printed? python print(int(1e23))