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))
  1. A. 1.0 100.0 1.5e-07 1.2345678901234568e+17 0.8
  2. B. 1.0 100.0 1.5e-07 123456789012345680.0 0.7999999999999999
  3. C. 1.0 100.0 1.5e-7 1.2345678901234568e+17 0.7999999999999999
  4. 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

Exams that ask this topic