ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
from fractions import Fraction as F
from decimal import Decimal as D
print(F(1, 3) + F(1, 6), D("0.1") + D("0.2") == D("0.3"), D(0.1) == D("0.1"), F(0.5))- A. 1/2 True True 1/2
- B. 3/6 True False 1/2
- C. 1/2 False False 0.5
- D. 1/2 True False 1/2
Correct answer: D. 1/2 True False 1/2
Fraction arithmetic is exact and reduced. Decimals built from strings are exact, but Decimal(0.1) captures the binary float's full value (0.1000000000000000055...), which differs from Decimal('0.1'). 0.5 is exact in binary.
More Python Programming questions
- What is printed (Python 3.12 or later)? python from functools import reduce xs = [0.1] 10 print(sum(xs) == 1.0, reduce(lambda a, b: a + b…
- What is printed? python nan = float("nan") print(max(nan, 1), max(1, nan), min(nan, 1), nan in {nan})
- What is printed? python print(max([1, 2], [1, 2, 0]), min("Z", "a", ""), sorted([True, 0, -1, 0.5]), max(3, 3.0), max(3.0, 3))
- What is printed? python print(bin(-5), (-5).bitlength(), ~5, -5 >> 1, -5 & 0b111, 5 ^ -1)
- What is printed? python print("10" + "5", int("10") + 5, "10" 2, str(10) 2 == "1010", "5" < "10")
- What is printed? python a = "hello" try: a[0] = "j" except TypeError: a = "j" + a[1:] b = a.replace("l", "X", 1) print(a, b…