ProgrammingHardProduct-company MCQ pattern

What is printed on the first line?

What is printed on the first line?
x = 0.1 * 3
y = 0.3
print(abs(x - y) < 1e-9, x > y, x - y, y - x)
print(float('0.1') * 3 == 0.3, 3 * float('0.1') > 0.3)
  1. A. True True 0.0 0.0
  2. B. False True 5.551115123125783e-17 -5.551115123125783e-17
  3. C. True False 5.551115123125783e-17 -5.551115123125783e-17
  4. D. True True 5.551115123125783e-17 -5.551115123125783e-17

Correct answer: D. True True 5.551115123125783e-17 -5.551115123125783e-17

0.1 * 3 is 0.30000000000000004, which is larger than 0.3 by one unit in the last place. A tolerance check is the safe way to compare, since the exact test fails.

More Python Programming questions

Exams that ask this topic