ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(0.1 + 0.7, 0.1 * 7, round(0.1 * 7 * 10), 3 * 0.1 == 0.30000000000000004)
- A. 0.8 0.7 7 True
- B. 0.7999999999999999 0.7000000000000001 7 True
- C. 0.7999999999999999 0.7 7 False
- D. 0.7999999999999999 0.7000000000000001 8 False
Correct answer: B. 0.7999999999999999 0.7000000000000001 7 True
Each float operation rounds to the nearest double, so 0.1 + 0.7 and 0.1 * 7 land on different neighbours of the decimal values. The last test is True because 3 * 0.1 is exactly the double written as 0.30000000000000004.
More Python Programming questions
- What is printed? python print((-1) 0.5, (-1) 2, (-8) (1 / 3), -1 0.5)
- Which three lines are printed? python import sys print(sys.getintmaxstrdigits()) try: int('1' 5000) print('parsed') except ValueError…
- What is printed? python import math print(math.isclose(0.1 + 0.2, 0.3), math.isclose(1e-12, 0), math.isclose(1e-12, 0, abstol=1e-9))
- What is printed? python print(int('١٢٣') + 1, 'ß'.upper(), 'Dž'.lower(), len('ß'.upper()))
- What is printed? python total = 0.0 for in range(10): total += 0.1 print(total, total == 1.0, sum([0.1] 10) == 1.0)
- Which of these is the best way to build a string from n pieces in CPython, and why?