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)- A. True True 0.0 0.0
- B. False True 5.551115123125783e-17 -5.551115123125783e-17
- C. True False 5.551115123125783e-17 -5.551115123125783e-17
- 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
- What is printed on the second line? python import math print(math.isclose(1e9, 1e9 + 1e-3), math.isclose(1e9, 1e9 + 1, reltol=1e-9))…
- What is printed on the first line? python print(0.1 + 0.2 - 0.3 == 0, 0.1 + 0.2 - 0.3) print(1 - 0.9 == 0.1, 1 - 0.9)
- What is printed? python import sys print(sys.floatinfo.epsilon, 1.0 + sys.floatinfo.epsilon > 1.0, 1.0 + sys.floatinfo.epsilon / 2 > 1.0)
- What is printed on the first line? python print(263 2-63 == 1.0, 264 + 1.0 - 264, (264 + 1) - 264) print(float(264 + 1) - float(264))
- What is printed on the first line? python print(7 % -3, -7 % 3, 7 % 3, -7 % -3) print(-7 // -3, 7 // -3, -7 // 3)
- What is printed on the second line? python def mod(a, m): r = a % m return r + m if r < 0 else r print(mod(-1, 5), mod(-5, 5), mod(-6, 5))…