wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Recursion

Total questions: 12

Worksheet time: 24mins

Name
Class
Date
1.

The process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself is known as:

a)

repetition

b)

recursion

c)

reapparition

d)

reversing

2.

Which of the following is the best definition of a recursive method?

a)

A method that iterates itself exactly 5 times.

b)

A method that invokes itself by name within the method.

c)

A method that will never iterate infinitely.

d)

A method that cannot be called more than once.

3.

Recursion is similar to which of the following?

a)

if-else

b)

switch-case

c)

loops

d)

none of the above

4.

Name the condition at which the recursive method will stop calling itself.

a)

Base case

b)

Worst Case

c)

Best Case

d)

None of the above

5.

The following function finds the factorial of any number


int factorial(int n)

{

if(n == 0 || n == 1) return 1;


return n * factorial(n-1);

}


What would calling factorial(4) output?

a)

24

b)

16

c)

8

d)

64

6.

The following function finds the factorial of any number


int factorial(int n)

{

if(n == 0 || n == 1) return 1;


return n * factorial(n-1);

}


What would calling factorial(2) output?

a)

12

b)

2

c)

0

d)

22

7.

Make the following function return the following number in the fibonacci sequence


int fibo(int n)

{

if(n == 0 || n == 1) return n;

return _________________;

}

a)

fib (n-1)+fib (n-2)

b)

fibo(n-1)+fibo(n-2)

c)

fibo(n*2)

d)

fibo(n+1)+fibo(n+2)

8.

True or False. Recursion can be used to fill in or print an array.

a)

True

b)

False

9.

What is the following recursive function missing?


int fun1(int x, int y){

return fun1(x - 1, x + y);

}

a)

A base case

b)

A general case

c)

a title

d)

A return statement

10.

What is the following recursive function missing?


int funTwo(int x, int y){

if (x == 0)

cout<< y;

else

cout<< fun1(x - 1, x + y);


}

a)

A base case

b)

A general case

c)

the braces

d)

A return statement

11.

Complete the following function to be able to print an array recursively:


void print_array(int arr[], int size)

{

int i;

if (i == size) {

i = 0;

cout << endl;

return;

}

cout << ______<< " ";

i++;

print_array(arr, size);

}

a)

arr[i]

b)

i

c)

size

d)

print-array

12.

Select all the examples of cases where recursive solutions are common

a)

sorting

b)

searching

c)

displaying an image

d)

setting variable values