ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = 0.1 + 0.2
print(f"{a:.20f}"[:6], a == 0.3, round(a, 15) == round(0.3, 15), str(a) == "0.30000000000000004", float.hex(0.5))
  1. A. 0.3000 False True True 0x1.0000000000000p-1
  2. B. 0.3000 True True True 0x1.0000000000000p-1
  3. C. 0.3000 False False True 0x1.0000000000000p-1
  4. D. 0.3000 False True False 0x0.8p+0

Correct answer: A. 0.3000 False True True 0x1.0000000000000p-1

The binary sum is slightly above 0.3, so == fails but rounding both to 15 places matches. str gives the shortest repr that round-trips. float.hex(0.5) is 1.0 x 2^-1.

More Python Programming questions

Exams that ask this topic