ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(float('0.1') + float('0.2') == 0.3, float('0.30000000000000004') == 0.1 + 0.2, repr(float('0.30000000000000004')), 0.1 + 0.2 == float(repr(0.1 + 0.2)))
  1. A. False True 0.3 True
  2. B. True True 0.30000000000000004 True
  3. C. False True 0.30000000000000004 True
  4. D. False False 0.30000000000000004 True

Correct answer: C. False True 0.30000000000000004 True

0.1 + 0.2 is the double 0.30000000000000004, which is not the double for 0.3. repr gives the shortest string that round-trips, so converting it back gives the same value.

More Python Programming questions

Exams that ask this topic