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!
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
Answer: b
Explanation: Recursion in Python is a technique where a function calls itself.
a) Base case
b) Iterative loop
c) Global variables
d) List comprehension
Answer: a
Explanation: A recursive function must have a base case to terminate the recursion.
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
Answer: d
Explanation: The base case is a condition that stops the recursive calls.
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
Answer: c
Explanation: Recursive functions can lead to infinite loops if the base case is not defined or implemented correctly.
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
Answer: a
Explanation: Without a base case, the recursive function will continue to call itself indefinitely.
a) Base case
b) Recursive case
c) Iterative loop
d) Function call to itself
Answer: c
Explanation: Recursive functions do not require an iterative loop; instead, they rely on function calls to itself.
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
Answer: d
Explanation: The factorial
function calculates the factorial of a number recursively. In this case, factorial(4)
returns 4 * 3 * 2 * 1 = 24.
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
Answer: a
Explanation: The correct syntax for a recursive function in Python involves calling the function within its own body, as shown in option (a).
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
Answer: c
Explanation: Recursion is preferred when a problem can be divided into smaller, similar sub-problems.
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
Answer: a
Explanation: The base case for the fibonacci
function is when n <= 1
, which returns n
directly.
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
Answer: b
Explanation: The power
function computes the exponential power of x
raised to n
.
a) List
b) Tuple
c) Dictionary
d) Linked list
Answer: d
Explanation: A linked list is a recursive data structure because each node contains a reference to another node.
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
Answer: a
Explanation: The count_down
function recursively prints the numbers from n
to 1, so the output will be 5, 4, 3, 2, 1.
a) Improved readability of code
b) Efficient memory usage
c) Limited depth due to stack overflow
d) Faster execution speed
Answer: c
Explanation: A disadvantage of recursion is that it can lead to stack overflow errors if the depth of recursion is too large.
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
Answer: b
Explanation: The sum_digits
function recursively sums the individual digits of a number.
a) n == 1
b) n == 0
c) n < 0
d) n > 1
Answer: b
Explanation: The base case for calculating the factorial of a number is when n == 0
.
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”
Answer: a
Explanation: The reverse_string
function recursively reverses the input string, so the output will be “olleh”.
a) To call the function itself
b) To handle exceptions
c) To define the termination condition
d) To optimize memory usage
Answer: c
Explanation: The base case in a recursive function defines the termination condition to end the recursion.
a) Bubble sort
b) Quick sort
c) Binary search
d) Linear search
Answer: b
Explanation: Quick sort is a recursive algorithm that divides an array into smaller sub-arrays.
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
Answer: d
Explanation: The mystery
function recursively adds numbers in steps of 2, starting from 5, so the output will be 5 + 3 + 1 = 9.
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
Answer: c
Explanation: Recursion requires a stack to keep track of function calls and can lead to stack overflow errors if the depth is too large.
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
Answer: b
Explanation: The countdown
function prints the numbers from n
to 1, so the output will be 3, 2, 1.
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
Answer: a
Explanation: The sum_even
function recursively sums even numbers up to n
, so the output will be 2 + 4 = 6.
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
Answer: d
Explanation: Recursive functions can lead to infinite recursion if the base case is not properly defined.
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
Answer: c
Explanation: The multiply
function recursively computes the product of a
and b
, so the output will be 4 * 5 = 20.
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
Answer: a
Explanation: A common pitfall of recursion is the risk of stack overflow due to excessive memory usage.
a) To handle exceptions
b) To define the base case
c) To call the function itself with smaller inputs
d) To terminate the recursion
Answer: c
Explanation: The recursive case in a recursive function calls the function itself with smaller inputs to solve the problem incrementally.
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
Answer: a
Explanation: Tail recursion is a type of recursion where the recursive call is the last thing executed by the function, optimizing memory usage.
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
Answer: a
Explanation: The sum_odd
function recursively sums odd numbers up to n
, so the output will be 5 + 3 + 1 = 9.
a) Binary search
b) Linear search
c) Bubble sort
d) Selection sort
Answer: a
Explanation: Binary search is a recursive algorithm that divides the search interval in half.
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
Answer: c
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.
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]
Answer: b
Explanation: The reverse_list
function recursively reverses a list, so the output will be [4, 3, 2, 1].
Congratulations on completing the Python Recursion MCQs! Recursion is a fundamental concept in programming that allows functions to call themselves and solve complex problems by breaking them down into simpler subproblems. By mastering recursion in Python, you gain the ability to solve a wide range of problems more elegantly and efficiently. Keep practicing and experimenting with recursive functions to become proficient in using this powerful technique. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!
You can also enroll in our free Python Course Today!
Read our more articles related to MCQs in Python: