The program below stops with an error. Which statement correctly describes the problem?
def factorial(n):
return n * factorial(n - 1)
print(factorial(5))
Every recursive function needs a case that returns without recursing.
Recursion · Exercise 3 of 4
Each recursive Python program has a mistake: no base case, a base case that is never reached, a missing return, a wrong base value, or a structural slip. Choose the option that names the real problem.
The program below stops with an error. Which statement correctly describes the problem?
def factorial(n):
return n * factorial(n - 1)
print(factorial(5))
Every recursive function needs a case that returns without recursing.
The program below stops with an error. Which statement correctly describes the problem?
def count_evens_down(n):
if n == 0:
return 0
return 1 + count_evens_down(n - 2)
print(count_evens_down(5))
Base cases written with == are fragile when the step is larger than 1.
factorial(5) should be 120. Which statement correctly identifies the bug?
def factorial(n):
if n == 0:
return 0
return n * factorial(n - 1)
print(factorial(5))
The base value must be the correct answer for the smallest input.
The program below stops with an error. Which statement correctly describes the problem?
def factorial(n):
if n <= 1:
return 1
result = n * factorial(n - 1)
print(factorial(3))
factorial(2) works out 2 * 1 but returns None; the caller then multiplies by None.
rev("abcd") should return "dcba". Which statement correctly identifies the bug?
def rev(s):
if len(s) <= 1:
return s
return s[0] + rev(s[1:])
print(rev("abcd"))
The order in which the parts are combined decides the result.
The function should check every pair of characters. Which statement correctly identifies the bug?
def is_pal(s):
if len(s) < 2:
return True
return s[0] == s[-1]
print(is_pal("abca"))
The recursive case must call the function on the smaller problem, the string without its ends.
The program below stops with an error. Which statement correctly describes the problem?
def total(n):
return 0 if n == 0 else n + total(n - 1)
print(total(10_000))
Python keeps every frame until the base case is reached, and it has no tail-call optimisation.
The program below stops with an error. Which statement correctly describes the problem?
from functools import lru_cache
@lru_cache(maxsize=None)
def total(nums):
return 0 if not nums else nums[0] + total(nums[1:])
print(total([3, 5, 7]))
With a tuple, slicing gives tuples too, so every recursive call stays hashable.
Python refuses to run the program below. Which statement correctly describes the problem?
def fib(n)
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(10))
Write def fib(n):.
The program below stops with an error. Which statement correctly describes the problem?
def largest(nums):
if len(nums) == 1:
return nums[0]
rest = largest(nums[1:])
return nums[0] if nums[0] > rest else rest
print(largest([4, 9, 2]))
print(largest([]))
The first call prints 9. For [] the recursive call happens before nums[0] is ever read, and slicing an empty list never fails – it just returns another empty list – so the input never shrinks.
Timed online tests with instant scores and explanations.