ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
from fractions import Fraction
print(Fraction('1.5'), Fraction('3/6'), float(Fraction('1/3')) == 1/3, str(Fraction(2, 4)))
  1. A. 3/2 1/2 False 2/4
  2. B. 3/2 3/6 True 1/2
  3. C. 3/2 1/2 True 2/4
  4. D. 3/2 1/2 True 1/2

Correct answer: D. 3/2 1/2 True 1/2

Fraction reduces to lowest terms, so 1.5 is 3/2, 3/6 is 1/2 and 2/4 becomes 1/2. float(Fraction('1/3')) rounds to the same double as 1/3.

More Python Programming questions

Exams that ask this topic