ProgrammingHardTCS NQT · Infosys
What is printed on the first line?
What is printed on the first line?
print(0.5 + 0.25 == 0.75, 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0) print(sum([0.1] * 3), 0.1 + 0.1 + 0.1 == sum([0.1] * 3))
- A. False True
- B. True True
- C. False False
- D. True False
Correct answer: D. True False
Halves and quarters are exact in binary, so 0.5 + 0.25 equals 0.75. Adding 0.1 ten times in sequence accumulates error, so the total is not exactly 1.0.
More Python Programming questions
- What is printed? python print(round(0.5), round(1.5), round(2.5), round(-2.5), round(2.5 + 1e-15))
- What is printed? python print(round(12345, -2), round(12350, -2), round(12250, -2), round(-1234.5, -1))
- What is printed? python print(format(1234.5678, '.2f'), format(1234.5678, '.2e'), format(0.000012345, '.3g'), format(12345.6789, ',.1f'))
- What is printed? python print(str(1.0), str(1e2), str(1.5e-7), str(123456789012345678.0), str(0.1 + 0.7))
- What is printed? python print(1 / 3 3 == 1.0, 2 / 3 3 == 2.0, 10 / 3 3 == 10.0, 0.7 3 == 2.1)
- What is printed? python print(float('nan') < 1, float('nan') > 1, float('nan') != float('nan'))