wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Recursion Quiz

Total questions: 25

Worksheet time: 38mins

Name
Class
Date
1.

What is recursion in Python?

a)

A loop that repeats until a condition is met

b)

A function that calls itself

c)

A function that uses iteration

d)

A class method that runs automatically

2.

Every recursive function must have:

a)

A loop

b)

A return statement

c)

A base case

d)

A recursive call

3.

What will happen if the base case is missing in recursion?

a)

Function runs once

b)

Syntax error

c)

Infinite recursion leading to a RecursionError

d)

The program will exit normally

4.

What is the output of the following code? def func(x): if x == 0: return 0 else: return x + func(x - 1)

a)

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.

b)

The function returns x squared (i.e., x * x). For example, func(5) returns 25.

c)

The function returns the factorial of x (i.e., x!). For example, func(5) returns 120.

d)

The function always returns 0 for any input.

5.

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?

a)

3

b)

6

c)

5

d)

4

6.

Which of the following statements about recursion is true?

a)

Recursion always uses less memory than iteration

b)

Recursion can replace any iterative solution

c)

Recursive solutions often use the call stack to remember state

d)

Recursion is faster than iteration

7.

Identify the base case in this function: def factorial(n): if n == 0: return 1 return n * factorial(n - 1)

a)

return n * factorial(n - 1)

b)

if n == 0:

c)

return 1

d)

Both B and C

8.

Given the following code: return 1 return n + rec_sum(n-1)

a)

Finds factorial

b)

Sums numbers from 1 to n

c)

Finds Fibonacci

d)

Squares numbers

9.

In tail recursion, the recursive call is:

a)

The first operation

b)

The last operation

c)

Both first and last

d)

Not used

10.

Why is tail recursion optimization not supported in Python?

a)

Python prefers loops

b)

To maintain traceback visibility for debugging

c)

Because recursion is unsafe

d)

Because of the GIL

11.

Output of: def power(x, y): if y == 0: return 1 return x * power(x, y - 1) print(power(2, 3))

a)

6

b)

8

12.

Recursive Fibonacci function has a time complexity of:

a)

O(n)

b)

O(log n)

c)

O(2n)O(2^n)

d)

O(n2)O(n^2)

13.

What is the output?

def count_down(n):

    if n == 0:

        print("Blast off!")

    else:

        print(n)

        count_down(n - 1)

count_down(3)

a)

3 2 1 Blast off!

b)

Blast off! 3 2 1

c)

3 2 1

d)

3 2 Blast off!

14.

Which data structure is used to manage function calls in recursion?

a)

Queue

b)

Stack

15.

What is the output of: def rec(n): if n == 1: return 1 return rec(n - 1) + 1 print(rec(5))

a)

A. 4

b)

B. 5

c)

C. 6

d)

D. None

16.

Which of the following problems is best solved using recursion?

a)

Printing numbers

b)

Calculating factorial

c)

Multiplying two numbers

d)

Finding average

17.

Which function type uses recursion internally?

a)

sum()

b)

map()

c)

max()

d)

len()

18.

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)

A. 2 4

b)

B. 5 4 3 2 1

c)

C. 4 2

d)

D. 2 4 6

19.

What will this function return?

def rec(n):

    if n == 0:

        return 0

    else:

        return n + rec(n-2)

print(rec(5))

a)

5

b)

8

c)

9

d)

7

20.

Recursion is most useful for problems that are:

a)

Iterative

b)

Repetitive

c)

Divisible into smaller subproblems

d)

Random

21.

Which of the following can replace recursion effectively?

a)

Queue

b)

Loop (iteration)

c)

Dictionary

d)

Function pointer

22.

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))

a)

6

b)

3

c)

12

d)

5

23.

What is the output of the following code? return 0 return n % 10 + sum_digits(n // 10) print(sum_digits(123))

a)

6

b)

5

c)

4

d)

7

24.

Recursive functions are stored in:

a)

Global memory

b)

Stack memory

c)

Heap memory

d)

Static memory

25.

Which one is an example of indirect recursion?

a)

A. A function calling itself

b)

B. Function A calling function B and B calling A

c)

C. Function A calling itself twice

d)

D. None