ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def f(n):
    try:
        if n:
            raise IndexError
        return "body"
    except IndexError:
        return "except"
    else:
        return "else"
    finally:
        print("fin", end=" ")
print(f(0), f(1))
  1. A. fin body fin except
  2. B. fin fin body except
  3. C. body fin except fin
  4. D. fin fin else except

Correct answer: B. fin fin body except

A return inside the try body skips the else clause. Both calls run finally (printing fin) before print receives its arguments, so both 'fin' appear before the returned values.

More Python Programming questions

Exams that ask this topic