# 30+ MCQs on Python Recursion

Ayushi Trivedi 06 Mar, 2024

Welcome to the Python Recursion Python Interview Questions! Recursion is a powerful programming technique where a function calls itself in order to solve a problem. In Python, recursion is commonly used to solve problems that can be broken down into smaller, similar subproblems. These questions will test your understanding of recursion in Python, including its basic principles, implementation, termination conditions, and examples. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python recursion together!

#### Q1. What is recursion in Python?

a) A loop structure used to repeat a sequence of instructions

b) A function calling itself

c) A method for checking data types

d) A way to handle exceptions in Python

Explanation: Recursion in Python is a technique where a function calls itself.

#### Q2. Which of the following must be defined in a recursive function?

a) Base case

b) Iterative loop

c) Global variables

d) List comprehension

Explanation: A recursive function must have a base case to terminate the recursion.

#### Q3. What is a base case in recursive functions?

a) The first case in a sequence

b) A case that returns the final output

c) A case where the function calls itself

d) A condition that stops the recursion

Explanation: The base case is a condition that stops the recursive calls.

#### Q4. Which of the following is true about recursive functions?

a) They are less memory-efficient than iterative solutions

b) They always run faster than iterative solutions

c) They can result in infinite loops if not implemented correctly

d) They are limited to handling numeric data types

Explanation: Recursive functions can lead to infinite loops if the base case is not defined or implemented correctly.

#### Q5. What happens if a recursive function does not have a base case?

a) The function will execute indefinitely

b) The function will return None

c) The function will raise an exception

d) The function will terminate immediately

Explanation: Without a base case, the recursive function will continue to call itself indefinitely.

#### Q6. Which of the following is NOT required for a recursive function?

a) Base case

b) Recursive case

c) Iterative loop

d) Function call to itself

Explanation: Recursive functions do not require an iterative loop; instead, they rely on function calls to itself.

#### Q7. What is the output of the following recursive function?

``````def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

result = factorial(4)
print(result)
``````

a) 1

b) 6

c) 12

d) 24

Explanation: The `factorial` function calculates the factorial of a number recursively. In this case, `factorial(4)` returns 4 * 3 * 2 * 1 = 24.

#### Q8. Which of the following is the correct syntax for a recursive function in Python?

a)

``````def recursive_function():
# Function body
recursive_function()
``````

b)

``````def recursive_function:
# Function body
recursive_function()
``````

c)

``````def recursive_function():
# Function body
return recursive_function()
``````

d)

``````def recursive_function():
# Function body
return recursive_function
``````

Explanation: The correct syntax for a recursive function in Python involves calling the function within its own body, as shown in option (a).

#### Q9. When is recursion generally preferred over iteration?

a) When the problem can be easily solved using loops

b) When the problem can be solved iteratively

c) When the problem can be divided into smaller, similar sub-problems

d) When the problem requires complex mathematical operations

Explanation: Recursion is preferred when a problem can be divided into smaller, similar sub-problems.

#### Q10. What is the base case in the following recursive function?

``````def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

result = fibonacci(5)
print(result)
``````

a) `n <= 1`

b) `n == 0`

c) `n == 1`

d) `n == 2`

Explanation: The base case for the `fibonacci` function is when `n <= 1`, which returns `n` directly.

#### Q11. What does the following recursive function compute?

``````def power(x, n):
if n == 0:
return 1
else:
return x * power(x, n-1)

result = power(2, 3)
print(result)
``````

a) Factorial of a number

b) Exponential power

c) Square root

d) Logarithm

Explanation: The `power` function computes the exponential power of `x` raised to `n`.

#### Q12. Which of the following represents a recursive data structure?

a) List

b) Tuple

c) Dictionary

Explanation: A linked list is a recursive data structure because each node contains a reference to another node.

#### Q13. What will be the output of the following recursive function?

``````def count_down(n):
if n > 0:
print(n)
count_down(n-1)

count_down(5)
``````

a) 5, 4, 3, 2, 1

b) 1, 2, 3, 4, 5

c) 1, 1, 1, 1, 1

d) 5, 5, 5, 5, 5

Explanation: The `count_down` function recursively prints the numbers from `n` to 1, so the output will be 5, 4, 3, 2, 1.

#### Q14. Which of the following is a disadvantage of using recursion?

a) Improved readability of code

b) Efficient memory usage

c) Limited depth due to stack overflow

d) Faster execution speed

Explanation: A disadvantage of recursion is that it can lead to stack overflow errors if the depth of recursion is too large.

#### Q15. What does the following recursive function do?

``````def sum_digits(n):
if n == 0:
return 0
else:
return n % 10 + sum_digits(n // 10)

result = sum_digits(123)
print(result)
``````

a) Computes the factorial of a number

b) Sums the digits of a number

c) Reverses a number

d) Finds the maximum digit in a number

Explanation: The `sum_digits` function recursively sums the individual digits of a number.

#### Q16. Which of the following is the correct base case for a recursive function that calculates the factorial of a number?

a) `n == 1`

b) `n == 0`

c) `n < 0`

d) `n > 1`

Explanation: The base case for calculating the factorial of a number is when `n == 0`.

#### Q17. What will be the output of the following recursive function?

``````def reverse_string(s):
if len(s) == 0:
return s
else:
return reverse_string(s[1:]) + s[0]

result = reverse_string("hello")
print(result)
``````

a) “olleh”

b) “hello”

c) “oellh”

d) “ollhe”

Explanation: The `reverse_string` function recursively reverses the input string, so the output will be “olleh”.

#### Q18. What is the purpose of the base case in a recursive function?

a) To call the function itself

b) To handle exceptions

c) To define the termination condition

d) To optimize memory usage

Explanation: The base case in a recursive function defines the termination condition to end the recursion.

#### Q19. Which of the following represents a recursive algorithm?

a) Bubble sort

b) Quick sort

c) Binary search

d) Linear search

Explanation: Quick sort is a recursive algorithm that divides an array into smaller sub-arrays.

#### Q20. What will be the output of the following recursive function?

``````def mystery(n):
if n <= 0:
return 0
else:
return n + mystery(n-2)

result = mystery(5)
print(result)
``````

a) 0

b) 3

c) 4

d) 6

Explanation: The `mystery` function recursively adds numbers in steps of 2, starting from 5, so the output will be 5 + 3 + 1 = 9.

#### Q21. Which of the following statements is true about recursion?

a) Recursion is always more memory-efficient than iteration

b) Recursion is limited in its application and cannot be used for all problems

c) Recursion requires a stack to keep track of function calls

d) Recursion is always faster than iteration

Explanation: Recursion requires a stack to keep track of function calls and can lead to stack overflow errors if the depth is too large.

#### Q22. What will be the output of the following recursive function?

``````def countdown(n):
if n == 0:
return
print(n)
countdown(n-1)

countdown(3)
``````

a) 1, 2, 3

b) 3, 2, 1

c) 0, 1, 2, 3

d) 3, 1, 0

Explanation: The `countdown` function prints the numbers from `n` to 1, so the output will be 3, 2, 1.

#### Q23. What is the output of the following recursive function?

``````def sum_even(n):
if n == 0:
return 0
else:
if n % 2 == 0:
return n + sum_even(n-1)
else:
return sum_even(n-1)

result = sum_even(5)
print(result)
``````

a) 6

b) 8

c) 10

d) 12

Explanation: The `sum_even` function recursively sums even numbers up to `n`, so the output will be 2 + 4 = 6.

#### Q24. Which of the following statements is true about recursive functions?

a) Recursive functions always use more memory than iterative solutions

b) Recursive functions are less readable than iterative solutions

c) Recursive functions are easier to debug than iterative solutions

d) Recursive functions can lead to infinite recursion if not designed properly

Explanation: Recursive functions can lead to infinite recursion if the base case is not properly defined.

#### Q25. What is the output of the following recursive function?

``````def multiply(a, b):
if b == 0:
return 0
else:
return a + multiply(a, b-1)

result = multiply(4, 5)
print(result)
``````

a) Division

b) Exponential power

c) Multiplication

d) Subtraction

Explanation: The `multiply` function recursively computes the product of `a` and `b`, so the output will be 4 * 5 = 20.

#### Q26. Which of the following is a common pitfall when using recursion?

a) Stack overflow due to excessive memory usage

b) Improved readability compared to iterative solutions

c) Reduced risk of infinite loops

d) Faster execution speed

Explanation: A common pitfall of recursion is the risk of stack overflow due to excessive memory usage.

#### Q27. What is the purpose of the recursive case in a recursive function?

a) To handle exceptions

b) To define the base case

c) To call the function itself with smaller inputs

d) To terminate the recursion

Explanation: The recursive case in a recursive function calls the function itself with smaller inputs to solve the problem incrementally.

#### Q28. What is tail recursion in Python?

a) A type of recursion where the recursive call is the last thing executed by the function

b) A type of recursion where the function calls itself multiple times

c) A type of recursion that does not require a base case

d) A type of recursion that uses a loop instead of recursive calls

Explanation: Tail recursion is a type of recursion where the recursive call is the last thing executed by the function, optimizing memory usage.

#### Q29. What will be the output of the following recursive function?

``````def sum_odd(n):
if n <= 0:
return 0
else:
if n % 2 != 0:
return n + sum_odd(n-1)
else:
return sum_odd(n-1)

result = sum_odd(6)
print(result)
``````

a) 9

b) 12

c) 15

d) 18

Explanation: The `sum_odd` function recursively sums odd numbers up to `n`, so the output will be 5 + 3 + 1 = 9.

#### Q30. Which of the following is a recursive algorithm?

a) Binary search

b) Linear search

c) Bubble sort

d) Selection sort

Explanation: Binary search is a recursive algorithm that divides the search interval in half.

#### Q31. What is the output of the following recursive function?

``````def sum_squares(n):
if n == 0:
return 0
else:
return n**2 + sum_squares(n-1)

result = sum_squares(3)
print(result)
``````

a) 5

b) 14

c) 9

d) 10

Explanation: The `sum_squares` function recursively calculates the sum of squares from `n` to 1, so the output will be 3^2 + 2^2 + 1^2 = 9.

#### Q32. What is the output of the following recursive function?

``````def reverse_list(lst):
if not lst:
return []
else:
return [lst[-1]] + reverse_list(lst[:-1])

result = reverse_list([1, 2, 3, 4])
print(result)
``````

a) [1, 2, 3, 4]

b) [4, 3, 2, 1]

c) [1, 4, 2, 3]

d) [4, 1, 3, 2]

Explanation: The `reverse_list` function recursively reverses a list, so the output will be [4, 3, 2, 1].