ProgrammingVery hardCAT · MBA placement
What is printed?
What is printed?
print(sum([0.1] * 10) == 1.0, sum([0.1] * 10), round(sum([0.1] * 10), 2))
- A. True 1.0 1.0
- B. False 0.9999999999999999 1.0
- C. False 1.0 1.0
- D. False 0.9999999999999999 0.99
Correct answer: A. True 1.0 1.0
Since Python 3.12, sum() of floats uses compensated (Neumaier) summation, so ten copies of 0.1 add to exactly 1.0. Plain left-to-right addition would give 0.9999999999999999.
More Python Programming questions
- What is printed? python x = (5) y = (5,) print(type(x).name, type(y).name, len(y), ((5),) == (5,))
- What is printed? python a = {1, 2, 3} b = {3, 4} print(a ^ b, a.issubset({1, 2, 3, 4}), sorted(a - b, reverse=True), b > {3})
- What is printed? python import math print(math.floor(-2.5), math.trunc(-2.5), -7.5 // 2, divmod(7.5, 2))
- What is printed? python def dfs(g, s): seen, out, st = set(), [], [s] while st: u = st.pop() if u in seen: continue seen.add(u)…
- What is printed? python def hanoi(n, a, b, c, out): if n == 0: return hanoi(n - 1, a, c, b, out) out.append((a, c)) hanoi(n - 1, b, a, c…
- What is printed? python from collections import deque def islands(g): R, C = len(g), len(g[0]) seen = set() cnt = 0 for r in range(R): for…