ProgrammingHardInfosys · Python · Advanced

What is printed?

What is printed?
def f(n):
    try:
        if n == 0:
            raise ValueError
        return 10 // (n - 1)
    except ZeroDivisionError:
        return -1
    except ValueError:
        return -2
    finally:
        print('F', n, end=' ')
print([f(i) for i in range(3)])
  1. A. [-2, -1, 10] F 0 F 1 F 2
  2. B. F 0 F 1 F 2 [-1, -1, 10]
  3. C. F 0 F 1 F 2 [-2, -1, 10]
  4. D. F 0 F 1 F 2 [-2, 10, 10]

Correct answer: C. F 0 F 1 F 2 [-2, -1, 10]

The comprehension runs fully (each finally prints) before print shows the list; n=0 raises ValueError (-2), n=1 divides by zero (-1), n=2 gives 10 // 1.

More Python Programming questions

Exams that ask this topic