ProgrammingVery hardProduct company OA

What is printed?

What is printed?
def f(x):
    try:
        r = 10 / x
    except ZeroDivisionError:
        r = "inf"
        return r
    else:
        r = round(r)
    finally:
        r = str(r) + "!"
    return r

print(f(0), f(4))
  1. A. inf! 2!
  2. B. inf 3!
  3. C. inf! 3!
  4. D. inf 2!

Correct answer: D. inf 2!

For f(0) the return expression r is evaluated ("inf") before finally runs; finally rebinds the name r but cannot change the value already chosen for return, so "inf" is returned. For f(4), r = 2.5, round uses banker's rounding (ties to even) giving 2, finally makes it "2!" and the final return sends that back.

More Python Programming questions

Exams that ask this topic