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))
  1. A. False True
  2. B. True True
  3. C. False False
  4. 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

Exams that ask this topic