ProgrammingModerateCAT · MBA placement
What is printed?
What is printed?
from fractions import Fraction x = Fraction(1, 3) + Fraction(1, 6) print(x, x.numerator, Fraction(6, -8))
- A. 1/2 1 -3/4
- B. 3/4 1 -1/2
- C. 1/2 2 -3/4
- D. 1/3 1 -3/4
Correct answer: A. 1/2 1 -3/4
Fraction keeps exact rationals and moves the sign to the numerator, so 6/-8 becomes -3/4. The sum 1/3 + 1/6 is exactly 1/2.
More Python Programming questions
- What is printed? python print(format(255, '08b'), format(255, '#x'), '{:>5}|{:<3}|'.format(42, 7))
- What is printed? python def max3(a): a = sorted(a) return max(a[-1] a[-2] a[-3], a[0] a[1] a[-1]) print(max3([1, 2, 3, 4]), max3([-10…
- What is printed? python def g(): try: yield 1 yield 2 finally: print('cleanup') it = g() print(next(it)) it.close() print(list(it))
- What is printed? python print('hello world'.translate({ord('l'): None, ord('o'): 'O'}))
- What is printed? python class N: def init(self, v): self.v = v self.nxt = None def hascycle(h): slow = fast = h steps = 0 while fast and…
- What is printed? python print(hash(-1), hash(-2), hash(-1) == -2, hash(1) == hash(1.0))