ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(str(1.0), str(1e2), str(1.5e-7), str(123456789012345678.0), str(0.1 + 0.7))
- A. 1.0 100.0 1.5e-07 1.2345678901234568e+17 0.8
- B. 1.0 100.0 1.5e-07 123456789012345680.0 0.7999999999999999
- C. 1.0 100.0 1.5e-7 1.2345678901234568e+17 0.7999999999999999
- D. 1.0 100.0 1.5e-07 1.2345678901234568e+17 0.7999999999999999
Correct answer: D. 1.0 100.0 1.5e-07 1.2345678901234568e+17 0.7999999999999999
str of a float uses the shortest repr that round-trips, and switches to exponent form once the value reaches 1e16. The last value shows the usual binary rounding of 0.1 + 0.7.
More Python Programming questions
- What is printed? python print(1 / 3 3 == 1.0, 2 / 3 3 == 2.0, 10 / 3 3 == 10.0, 0.7 3 == 2.1)
- What is printed? python print(float('nan') < 1, float('nan') > 1, float('nan') != float('nan'))
- What is printed? python import math print(math.isnan(float('nan') 0), math.isinf(1e308 10), math.isfinite(-0.0), math.copysign(1, -0.0))
- What is printed on the first line? python print(min(0.0, -0.0), max(-0.0, 0.0), min(-0.0, 0.0), -0.0 < 0.0)
- What is printed? python print(sum(i (-1) i for i in range(1, 7)), (-1) 7 + 0.0, (-1.0) 2, (-2) -2)
- What is printed? python def f(n): return sum(d if n % 2 else -d for d in range(1, n + 1)) print(f(1), f(2), f(3), f(4))