ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def outer():
    results = []
    def rec(n):
        if n == 0:
            results.append("x")
            return
        for _ in range(n):
            rec(n - 1)
    rec(3)
    return len(results)
print(outer())
  1. A. 6
  2. B. 3
  3. C. 9
  4. D. 27

Correct answer: A. 6

rec(n) makes n calls to rec(n-1), so the number of leaves is n! = 3 * 2 * 1 = 6.

More Python Programming questions

Exams that ask this topic