ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
x = 0.1 + 0.7
print(x, x * 10, int(x * 10), round(x * 10))
  1. A. 0.7999999999999999 7.999999999999999 7 8
  2. B. 0.8 8.0 8 8
  3. C. 0.7999999999999999 8.0 8 8
  4. D. 0.7999999999999999 7.999999999999999 8 7

Correct answer: A. 0.7999999999999999 7.999999999999999 7 8

0.1 + 0.7 is just below 0.8, and multiplying by 10 keeps the shortfall. int() truncates 7.999... to 7, while round() goes to the nearest integer, 8.

More Python Programming questions

Exams that ask this topic