ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import sys
sys.setrecursionlimit(50)
def f(n):
    return 0 if n == 0 else 1 + f(n - 1)
try:
    print(f(60))
except RecursionError:
    print('deep')
  1. A. 50
  2. B. deep
  3. C. 60
  4. D. RecursionError

Correct answer: B. deep

f(60) needs about 61 nested Python frames. Because the limit is 50, a RecursionError is raised, and the except block prints 'deep'. The exception is not caught by the function itself.

More Python Programming questions

Exams that ask this topic