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))
- A. 0.7999999999999999 7.999999999999999 7 8
- B. 0.8 8.0 8 8
- C. 0.7999999999999999 8.0 8 8
- 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
- What is printed? python print(int('3' 3) + int('4' 2) 2, '3' 3 + '4' 2 2)
- What is printed? python print(len('%s' % 1.0), len('%g' % 1.0), len(repr(1.0)), str(1e5), '%g' % 1e5, '%g' % 1e6)
- What is printed? python def f(s): return s[::-1] == s print(f('abba'), f('ab' 2), f(''), f('a'))
- What is printed? python print(True + True, str(True) + '1', str(None), str([None]), repr('None'))
- What is printed? python print(sorted([0.5, -0.0, 0.0, -1]))
- What is printed? python x = 10 16 print(float(x) == x, float(x + 1) == x, x + 1.0 == x, (x + 1) - x)