ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def f(x):
    try:
        r = 10 // x
    except ZeroDivisionError:
        return "Z"
    else:
        return r
    finally:
        print("F", end=" ")

print(f(0), f(3))
  1. A. Z F 3 F
  2. B. F Z F 3
  3. C. F F Z 3
  4. D. Z 3 F F

Correct answer: C. F F Z 3

Both arguments of the outer print are evaluated first. Each call runs its finally block, which prints "F " before the function actually returns. f(0) returns "Z" and f(3) returns 10 // 3 = 3 (from the else block). Only then does print write "Z 3".

More Python Programming questions

Exams that ask this topic