NEW
Font size
WorksheetsRecursion Quiz
Total questions: 25
Worksheet time: 38mins
What is recursion in Python?
A loop that repeats until a condition is met
A function that calls itself
A function that uses iteration
A class method that runs automatically
Every recursive function must have:
A loop
A return statement
A base case
A recursive call
What will happen if the base case is missing in recursion?
Function runs once
Syntax error
Infinite recursion leading to a RecursionError
The program will exit normally
What is the output of the following code? def func(x): if x == 0: return 0 else: return x + func(x - 1)
The function returns the sum of all integers from 1 to x (i.e., it computes the sum 1 + 2 + ... + x). For example, func(5) returns 15.
The function returns x squared (i.e., x * x). For example, func(5) returns 25.
The function returns the factorial of x (i.e., x!). For example, func(5) returns 120.
The function always returns 0 for any input.
def func(x):
if x == 0:
return 0
else:
return x + func(x - 1)
print(func(3))
What is the output of the above code?
3
6
5
4
Which of the following statements about recursion is true?
Recursion always uses less memory than iteration
Recursion can replace any iterative solution
Recursive solutions often use the call stack to remember state
Recursion is faster than iteration
Identify the base case in this function: def factorial(n): if n == 0: return 1 return n * factorial(n - 1)
return n * factorial(n - 1)
if n == 0:
return 1
Both B and C
Given the following code: return 1 return n + rec_sum(n-1)
Finds factorial
Sums numbers from 1 to n
Finds Fibonacci
Squares numbers
In tail recursion, the recursive call is:
The first operation
The last operation
Both first and last
Not used
Why is tail recursion optimization not supported in Python?
Python prefers loops
To maintain traceback visibility for debugging
Because recursion is unsafe
Because of the GIL
Output of: def power(x, y): if y == 0: return 1 return x * power(x, y - 1) print(power(2, 3))
6
8
Recursive Fibonacci function has a time complexity of:
O(n)
O(log n)
O(2n)
O(n2)
What is the output?
def count_down(n):
if n == 0:
print("Blast off!")
else:
print(n)
count_down(n - 1)
count_down(3)
3 2 1 Blast off!
Blast off! 3 2 1
3 2 1
3 2 Blast off!
Which data structure is used to manage function calls in recursion?
Queue
Stack
What is the output of: def rec(n): if n == 1: return 1 return rec(n - 1) + 1 print(rec(5))
A. 4
B. 5
C. 6
D. None
Which of the following problems is best solved using recursion?
Printing numbers
Calculating factorial
Multiplying two numbers
Finding average
Which function type uses recursion internally?
sum()
map()
max()
len()
What is printed by this code?
def print_even(n):
if n == 0:
return
if n % 2 == 0:
print(n)
print_even(n - 1)
print_even(5)
A. 2 4
B. 5 4 3 2 1
C. 4 2
D. 2 4 6
What will this function return?
def rec(n):
if n == 0:
return 0
else:
return n + rec(n-2)
print(rec(5))
5
8
9
7
Recursion is most useful for problems that are:
Iterative
Repetitive
Divisible into smaller subproblems
Random
Which of the following can replace recursion effectively?
Queue
Loop (iteration)
Dictionary
Function pointer
28. What is the output of the following code? def sum_digits(n): if n == 0: return 0 else: return n % 10 + sum_digits(n // 10) print(sum_digits(123))
6
3
12
5
What is the output of the following code? return 0 return n % 10 + sum_digits(n // 10) print(sum_digits(123))
6
5
4
7
Recursive functions are stored in:
Global memory
Stack memory
Heap memory
Static memory
Which one is an example of indirect recursion?
A. A function calling itself
B. Function A calling function B and B calling A
C. Function A calling itself twice
D. None
