LearnFrenzy
Sign in Take a free test

Recursion · Exercise 3 of 4

Recursion Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
def factorial(n):
    return n * factorial(n - 1)

print(factorial(5))
A It prints 120
B RecursionError: there is no base case, so the calls continue past 0 into negative numbers until the depth limit; add if n <= 1: return 1
C It prints 0
D TypeError
Question page & discussion
Question 2 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
def count_evens_down(n):
    if n == 0:
        return 0
    return 1 + count_evens_down(n - 2)

print(count_evens_down(5))
A RecursionError: from 5 the values are 3, 1, -1 … – they jump over 0, so the base case is never met; use if n <= 0
B It prints 2
C It prints 3
D ValueError
Question page & discussion
Question 3 Easy

factorial(5) should be 120. Which statement correctly identifies the bug?

main.py
def factorial(n):
    if n == 0:
        return 0
    return n * factorial(n - 1)

print(factorial(5))
A n - 1 should be n + 1
B The function needs a loop
C The base case returns 0, and multiplying by 0 makes every result 0; 0! is 1
D print shows the wrong value
Question page & discussion
Question 4 Medium

The program below stops with an error. Which statement correctly describes the problem?

main.py
def factorial(n):
    if n <= 1:
        return 1
    result = n * factorial(n - 1)

print(factorial(3))
A TypeError: the recursive case computes result but never returns it, so factorial(2) returns None and 3 * None fails; add return result
B It prints 6
C RecursionError
D It prints None
Question page & discussion
Question 5 Medium

rev("abcd") should return "dcba". Which statement correctly identifies the bug?

main.py
def rev(s):
    if len(s) <= 1:
        return s
    return s[0] + rev(s[1:])

print(rev("abcd"))
A The base case should be len(s) == 0
B Slicing s[1:] removes the wrong character
C The first character is put back in front; it must go after the reversed rest: rev(s[1:]) + s[0]
D Strings cannot be reversed recursively
Question page & discussion
Question 6 Medium

The function should check every pair of characters. Which statement correctly identifies the bug?

main.py
def is_pal(s):
    if len(s) < 2:
        return True
    return s[0] == s[-1]

print(is_pal("abca"))
A The base case should be len(s) < 1
B s[-1] is the first character
C It compares only the outer characters and never recurses on the middle; return s[0] == s[-1] and is_pal(s[1:-1])
D The function should use a loop instead
Question page & discussion
Question 7 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
def total(n):
    return 0 if n == 0 else n + total(n - 1)

print(total(10_000))
A RecursionError: the logic is right, but 10,000 nested calls exceed Python's default limit of about 1000; use a loop or sum(range(n + 1))
B It prints 50005000
C MemoryError
D OverflowError
Question page & discussion
Question 8 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
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]))
A It prints 15
B RecursionError
C TypeError: lru_cache uses the arguments as dict keys and a list is unhashable; pass a tuple, e.g. total((3, 5, 7))
D IndexError
Question page & discussion
Question 9 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
def fib(n)
    return n if n < 2 else fib(n - 1) + fib(n - 2)

print(fib(10))
A The def header must end with a colon
B A function cannot call itself
C The conditional expression is invalid
D fib needs a base case before return
Question page & discussion
Question 10 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
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([]))
A IndexError: nums[0] on an empty list
B It prints 9 and then None
C RecursionError: an empty list never reaches len == 1, and [][1:] is again an empty list, so the function calls itself forever; handle the empty list first (return None or raise ValueError)
D TypeError
Question page & discussion
Up next · Exercise 4Recursion Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links