ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
x = 0.1 * 3 y = 0.3 print(x == y, abs(x - y) < 1e-15, round(x, 15) == round(y, 15), x - y)
- A. False True True 5.551115123125783e-17
- B. True True True 0.0
- C. False False True 5.55e-17
- D. False True False 0.0
Correct answer: A. False True True 5.551115123125783e-17
0.1 * 3 is one unit in the last place above 0.3, so the direct comparison fails. The difference is about 5.55e-17, which is within 1e-15 and disappears when both are rounded to 15 places.
More Python Programming questions
- What is printed? python print('ß'.isupper(), 'Dž'.istitle(), 'Dž'.isupper(), '1A'.isupper())
- What is printed? python print(f'{270:,}'[:8], len(f'{270:,}'), str(270)[-3:], ('%d' % 270)[-3:])
- What is printed? python data = [('x', 3), ('y', 1), ('z', 3)] print(max(data, key=lambda t: t[1]), min(data, key=lambda t: -t[1]))
- What is printed? python print(0.1 + 0.2 == 0.30000000000000004, 0.1 + 0.2 - 0.30000000000000004 == 0, '%.20f' % (0.1 + 0.2))
- What is printed? python s = '42' print(s 2 + '!' if s.isdigit() else '', int(s) 2, s.zfill(4) + '|' + s.rjust(4, '0') + '|' + s.ljust(4…
- What is printed? python print(sum(map(int, '123456789')) == 45, sum(map(int, str(99))), 99 % 9)