ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def f(x):
    try:
        return int(x)
    except ValueError:
        return float(x)
    except TypeError:
        return -1
print(f("3"), f("3.5"), f(None), f(2.9))
  1. A. 3 3.5 -1 2.9
  2. B. 3 ValueError -1 2
  3. C. 3 3.5 -1 2
  4. D. 3 3 -1 2

Correct answer: C. 3 3.5 -1 2

int('3.5') raises ValueError, so float('3.5') is returned. int(None) raises TypeError. int(2.9) truncates the float to 2 without raising.

More Python Programming questions

Exams that ask this topic