ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
from fractions import Fraction print(Fraction(1, 2) + 0.5 == 1, Fraction(1, 3) * 3 == 1, type(Fraction(1, 2) + 1).__name__, type(Fraction(1, 2) + 0.5).__name__)
- A. True False Fraction float
- B. True True float float
- C. True True Fraction float
- D. True True Fraction Fraction
Correct answer: C. True True Fraction float
Fraction arithmetic is exact, so the second check is True. Adding an int keeps the result a Fraction, but adding a float converts the result to float.
More Python Programming questions
- What is printed? python print(max(-3, 2, key=abs), min(-3, 2, key=abs), sorted([-3, 2, -2], key=abs), sorted([-3, 2, -2], key=lambda v…
- What is printed? python print(1 - 0.9 == 0.1, round(1 - 0.9, 1) == 0.1, 1 - 0.9 > 0.09, 1 - 0.9)
- What is printed? python print(len('a1b22c333'.split('2')), ''.join(c for c in 'a1b22c333' if c.isdigit()), len('a1b22c333'))
- What is printed? python print(0.1 + 0.1 + 0.1 == 0.3, 0.1 3, 0.3 / 0.1, int(0.3 / 0.1))
- What is printed? python print(format(0.5, '.0f'), format(1.5, '.0f'), format(2.5, '.0f'), format(-0.5, '.0f'))
- What is printed? python print('ab' 2 == 'abab', 'ab' 0 == '', 'a' 2 + 'b' 0 == 'aa', ('x' 2).count('xx'))