ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
def f(n):
    if n == 0:
        return 0
    return 1 + f(n - 1)
print(f(100))
  1. A. 99
  2. B. 100
  3. C. 101
  4. D. RecursionError

Correct answer: B. 100

This counts down from 100 to 0, adding 1 at each level of recursion; it correctly computes 100, and Python's default recursion limit (around 1000) comfortably allows 100 levels of recursion.

More Python Programming questions

Exams that ask this topic