wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Understanding Recursion

Total questions: 86

Worksheet time: 43mins

Name
Class
Date
1.

What is the minimum number of steps to solve a Tower of Hanoi puzzle with n disks?

a)

2n - 1

b)

n^2

c)

n!

d)

n + 1

2.

What is the first step in the algorithm for Tower of Hanoi with 2 disks?

a)

Move the larger disk to the destination peg.

b)

Move the smaller disk to the aux peg.

c)

Move both disks to the destination peg.

d)

Move the smaller disk to the source peg.

3.

What is the base case for the recursive algorithm of Tower of Hanoi?

a)

If disk == 0

b)

If disk == 1

c)

If disk == 2

d)

If disk == 3

4.

What does the Fibonacci series start with?

a)

0 and 1

b)

1 and 1

c)

0 and 0

d)

1 and 0

5.

What is the recursive formula for Fibonacci series?

a)

Fn = Fn-1 + Fn-2

b)

Fn = Fn-1 * Fn-2

c)

Fn = Fn-1 - Fn-2

d)

Fn = Fn-1 / Fn-2

6.

What is the first step in the divide-and-conquer approach?

a)

Merge/Combine

b)

Conquer/Solve

c)

Divide/Break

d)

None of the above

7.

What is a common data structure used for sorting algorithms?

a)

Arrays

b)

Trees

c)

Graphs

d)

Stacks

8.

What is the main advantage of the divide-and-conquer approach?

a)

It supports parallelism.

b)

It is always faster than other methods.

c)

It requires less memory.

d)

It is easier to implement.

9.

What is the time complexity of the Fibonacci series using recursion?

a)

O(n log n)

b)

O(2^n)

c)

O(n)

d)

O(1)

10.

In the divide-and-conquer approach, what is the purpose of the 'conquer' step?

a)

To combine results

b)

To solve subproblems

c)

To divide the problem

d)

To analyze the complexity

11.

Which sorting algorithm is based on the divide-and-conquer strategy?

a)

Bubble Sort

b)

Insertion Sort

c)

Quick Sort

d)

Selection Sort

12.

A recursive function is

a)

A function that calls other functions in a recursive way.

b)

Any function that calls itself is called recursive

c)

A function that has a base case or termination condition

d)

None of the above

13.

Recursion is:

a)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition.

b)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls other function in a step.

c)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having no termination condition.

d)

None of the above

14.

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.

15.

What is the definition of recursion?

a)

Recursion is a programming technique you can use to allow a method to have numerous fields in its argument.

b)

Recursion is programming technique you can use in which a method calls itself to solve a problem.

c)

Recursion is a programming language model organized around objects rather than "actions" and data rather than logic

d)

Recursion is a program that translates a source program written in some high-level programming language into machine code.

16.

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

17.

The program may run out of memory in a

a)

non-recursive function call

b)

recursive function call

c)

condition when too many variables are declared

d)

none of the above

18.

Which of the following condition is true?

a)

Recursion is always better than iteration.

b)

Recursion uses more memory as compared to iteration.

c)

Recursion uses less memory as compared to iteration.

d)

Iteration is always better and simpler than recursion.

19.

Which of the following problems can be solved using recursion?

a)

finding Nth number of the Fibonacci sequence

b)

finding the factorial of a number

c)

finding the length of a string

d)

all of the above

20.

Recursion is similar to which of the following?

a)

if-else

b)

switch-case

c)

loops

d)

none of the above

21.

What would happen if the base case if not defined in the recursive method?

a)

Stack Overflow

b)

Stack Underflow

c)

Program Crashes

d)

None of the above

22.

The number of recursive calls is limited to the ____ of the stack.

a)

time

b)

ability

c)

quality

d)

size

23.

Which of the following sorting algorithms use recursion?

a)

Selection Sort

b)

Insertion Sort

c)

Mergesort

24.

What is the returned value of recmethod(5)?

a)

68

b)

70

c)

75

d)

82

25.

What is printed as a result of the call stri("COMPSCI")?

a)

COMPSCI

COMPSC

COMPS

COMP

COM

CO

C

b)

COMPSCI

OMPSCI

MPSCI

PSCI

SCI

CI

I

c)

CO

COM

COMP

COMPS

COMPSC

COMPSCI

d)

C

CO

COM

COMP

COMPS

COMPSC

COMPSCI

26.

Will rec(5) iterate infinitely?

a)

Yes

b)

No

27.

What value is returned as a result of the call mystry(x)?

a)

x * (x+1)

b)

2x

c)

2(x-1)

d)

x3 +1

28.

What will print?

a)

1

2

11

b)

1

2

21

c)

1

2

3

11

d)

1

2

3

21

29.
What will print?
a)
nothing
b)
5
c)
6
d)
an error message
30.

Which of the following cannot be converted in a recursion function?

a)

Factorial program

b)

random function generation

c)

binary search

d)

Greatest common divisor(GCD)

31.

In a recursive function ,_______ case must always be reachable.

a)

recursive case

b)

Explicit case

c)

Base case

d)

repetitive case

32.

Recursion is:

a)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition.

b)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls other function in a step.

c)

is a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having no termination condition.

d)

None of the above

33.

A recursive function is

a)

A function that calls other functions in a recursive way.

b)

Any function that calls itself is called recursive

c)

A function that has a base case or termination condition

d)

None of the above

34.

In recursion a base case is:

a)

The most simplistic code of a program

b)

is a non-recursive function

c)

Is a problem that we know the answer to

d)

What stops the recursion from continuing on forever

35.

In recursion the sequence of smaller problems must eventually converge on?.

a)

an even smaller problem

b)

sorting algorithm

c)

the base case

d)

the main function of your program

36.

Which one is not a reason to use recursion?.

a)

One of the sub-tasks to be solved is nothing more than a simpler version of the same problem you are trying to solve in first place

b)

Code is generally shorter and easier to write than iterative code.

c)

Recursion is most useful for tasks that can be defined in terms of similar sub-tasks

d)

Low computational complexity

37.

Recursive thinking is:

a)

The use of recursion.

b)

Is the expectation to find a simpler version of the large problem during the design process.

c)

Is a useful technique borrowed from mathematics

d)

Solving a problem that is too complex

38.

infinite recursion is:

a)

A recursion function with approximation to + infinite.

b)

Is a myth

c)

When every recursive call produces another recursive call, then a recursive call will, in theory, run forever.

d)

None of the above

39.

Recursion is very efficient

a)

True

b)

False

40.

There is not overhead in the use of recursion

a)

True

b)

False

41.

A frame is

a)

A special type of function

b)

A recursive function with limited number of iterations

c)

Certain amount of memory that is set aside for functions to use, for purposes such as storing local variables

d)

None of the above

42.
What is the returned value of recMethod(5)?
a)
68
b)
70
c)
75
d)
82
43.

What is the action of method mystery2?

a)

a+b

b)

a*b

c)

ab

d)

ba

e)

a!

44.
What is printed as a result of the call stringMaker("COMPSCI")?
a)
COMPSCI
COMPSC
COMPS
COMP
COM
CO
C
b)
COMPSCI
OMPSCI
MPSCI
PSCI
SCI
CI
I
c)
CO
COM
COMP
COMPS
COMPSC
COMPSCI
d)
C
CO
COM
COMP
COMPS
COMPSC
COMPSCI
45.

Why would you use a base case?

a)

So that the code loops forever

b)

So that the code does not loop forever

c)

Because coding

46.
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.
47.

What is returned by the call mystery(3,3)?

a)

5

b)

6

c)

7

d)

Stack Overflow

48.

Which call to mystery would cause infinite recursion?

a)

mystery(1,2)

b)

mystery(2,2)

c)

mystery(3,2)

d)

none of these would cause infinite recursion

49.

What is the output of this program?

a)

2

b)

3

c)

4

d)

ArrayIndexOutOfBoundsException

50.

What is the output of this program?

a)

2

b)

3

c)

4

d)

ArrayIndexOutOfBoundsException

51.

What would happen if the base case if not defined in the recursive method?

a)

Stack Overflow

b)

Stack Underflow

c)

Program Crashes

d)

None of the above

52.

What is recursion in Java?

a)

a class

b)

a process of defining a method that calls other methods repeatedly

c)

a process of defining a method that calls itself repeatedly

d)

a process of defining a method that calls other methods which in turn call again this method

53.

Consider the following method.

public String goAgain(String str, int index)

{

if (index >= str.length())

return str;

return str + goAgain(str.substring(index), index + 1);

}

What is printed as a result of executing the following statement?

System.out.println(goAgain("today", 1));

a)

today

b)

todayto

c)

todayoday

d)

todayodayay

e)

todayodaydayayy

54.

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information

Consider the following binarySearch method. The method correctly performs a binary search.

Consider the following code segment.

int [ ] values = {1, 2, 3, 4, 5, 8, 8, 8};int target = 8;

What value is returned by the call binarySearch (values, target) ?

a)

-1

b)

3

c)

5

d)

6

e)

8

55.

Consider the following method.

public static int mystery(int n)

{

if (n <= 0)

{

return 0;

}

else

{

return n + mystery(n – 2);

}

}

What value is returned as a result of the call mystery(9) ?

a)

0

b)

9

c)

16

d)

24

e)

25

56.

static int doSomething(int a, int b){

if(b==1)

return a; else{

return a+ doSomething(a, b-1); }

What will be the output of the doSomething() method if a = 5 and b = 6?

a)

StackOverFlowException

b)

5

c)

1

d)

30

e)

6

57.

static void print(int x){

if(x==0) return;

System.out.print(x%2); print(x/2); }

What will be the output of the above code segment if the print() method is called with the parameter 9?

a)

001

b)

100

c)

1001

d)

101

e)

1110

58.

Which of the following cannot be solved using recursion?

I. code with nested iteration

II. code with nested if-else

III. code without a base case

a)

I only

b)

II only

c)

III only

d)

I and II

e)

II and III

59.

Which of the following give(s) a disadvantage of a recursive method compared with an iterative method?

I. extra memory space

II. small code size

III. more execution time

a)

I only

b)

II only

c)

III only

d)

I and II

e)

I and III

60.

use of the divide and conquer approach to arrange array elements

a)

base case

b)

merge sort

c)

indirect recursion

d)

void recursive method

61.

Consider the following code segment.


String one = "ABC123";

String two = "C";

String three = "3";

System.out.println(one.indexOf(two));

System.out.println(one.indexOf(three));

System.out.println(two.indexOf(one));


What is printed when the code segment is executed?

a)

2

5

-1

b)

2

5

2

c)

2

6

-1

d)

3

6

-1

e)

-1

-1

2

62.

Consider the following C-program

void foo (int n, int sum)

{

int k=0, j=0;

if (n==0) return;

k = n % 10;

j = n/10;

sum = sum + k;

foo (j, sum);

printf("%d", k);

}


int main()

{

int a=2048, sum=0;

foo (a, sum);

printf("%d\n", sum);

return 0;

}

What is the output?

a)

2 0 4 0 8

b)

2 0 4 8 0

c)

2 0 0 4 8

d)

2 0 8 0 4

63.

Consider the following C-program

int f(int n)

{

static int i=1;

if(n>5) return n;

n=n+i;

i++;

return f(n);

}


int main()

{

int a;

a=f(1);

printf("%d",a);

return 0;

}

What is the output?

a)

5

b)

6

c)

7

d)

8

64.

What is the return value of f(p, p), if the value of p is initialized to 5 before the call?

int f(int &x, int c)

{

c = c—1;

if (c==0) return 1;

x = x + 1;

return f(x,c) * x;

}


int main()

{

int a=5;

printf("%d",f(a,a));

return 0;

}

a)

6561

b)

6651

c)

5661

d)

6661

65.

Consider the following C-program


int fun(int n)

{

int x=1, k;

if (n==1) return x;

for (k=1; k<n; ++k)

{

x = x+fun(k) * fun(n—k);

}

return x;

}


int main()

{

int a;

a=fun(5);

printf("%d",a);

return 0;

}

What is the output?

a)

52

b)

15

c)

50

d)

51

66.

Consider the following C-program

void count(int n)

{

static int d = 1;

printf("%d", n);

printf("%d", d);

d++;

if (n > 1) count (n-1);

printf("%d", d);

}


void main()

{

count (3);

}

What is the Output?

a)

321231444

b)

322113444

c)

312213444

d)

321213444

67.

What is the output of the following code?


void my_recursive_function(int n)

{

if(n == 0)

return;

printf("%d ",n);

my_recursive_function(n-1);

}

int main()

{

my_recursive_function(10);

return 0;

}

a)

10

b)

1

c)

10 9 8 7 6 5 4 3 2 1 0

d)

10 9 8 7 6 5 4 3 2 1

68.

What will be the output of the following C code?


main()

{

int n;

n=f1(4);

printf("%d",n);

}

f1(int x)

{

int b;

if(x==1)

return 1;

else

b=x*f1(x-1);

return b;

}

a)

2

b)

24

c)

12

d)

10

69.

Predict output of following program


int fun(int n)

{

if (n == 4)

return n;

else return 2*fun(n+1);

}

int main()

{

printf("%d ", fun(2));

return 0;

}

a)

4

b)

16

c)

8

d)

Error

70.

in the tower of hanoi problem which disk is shifted first

a)

Bottom one

b)

Top one

c)

N-1 th Disk

d)

none of the options are correct

71.

What will be the output of the code?

a)

Print First 3

Print First 2

Print First 1

Print Last 2

Print Last 3

Print Last 4

b)

Print First 1

Print First 2

Print First 3

Print Last 2

Print Last 3

Print Last 4

c)

Print First 3

Print First 2

Print First 1

Print Last 1

Print Last 2

Print Last 3

d)

Print First 3

Print First 2

Print First 1

Print Last 1

Print Last 3

Print Last 4

72.

What will be the output of the program

a)

90

b)

92

c)

81

d)

91

73.

Consider a definition of mystery():

mystery(0,N) = N

mystery(P,Q) = mystery(P-1, Q+1)

According to this definition, what is mystery(2,4)?

a)

3

b)

4

c)

5

d)

6

74.

Say that you have a recursive Java method, funct() . Is it always possible to write an iterative version of funct() ?

a)

Yes

b)

Usually, but not always.

c)

Almost never.

d)

No.

75.

What will be displayed by the method call count(10,20)?

a)

10 11 12 13 14 15 16 17 18 19 20

b)

10 11 12 13 14 15 16 17 18 19

c)

20 19 18 17 16 15 14 13 12 11 10

d)

19 18 17 16 15 14 13 12 11 10

e)

20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20

76.

What will be displayed by the method call count(10,20)?

a)

10 11 12 13 14 15 16 17 18 19 20

b)

10 11 12 13 14 15 16 17 18 19


c)

20 19 18 17 16 15 14 13 12 11 10

d)

19 18 17 16 15 14 13 12 11 10

e)

20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20

77.

What will be displayed by the method call count(10,20) ?

a)

10 11 12 13 14 15 16 17 18 19 20

b)

10 11 12 13 14 15 16 17 18 19

c)

20 19 18 17 16 15 14 13 12 11 10

d)

10 11 12 13 14 15 16 17 18 19 20 20 19 18 17 16 15 14 13 12 11 10

e)

20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20

78.

What will be displayed by the method call count(10,20) ?

a)

10 11 12 13 14 15 16 17 18 19 20

b)

10 11 12 13 14 15 16 17 18 19

c)

20 19 18 17 16 15 14 13 12 11 10

d)

19 18 17 16 15 14 13 12 11 10

e)

20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20

79.

What value does mystery1(5) return ?

a)

0

b)

1

c)

4

d)

5

e)

6

80.

What value does mystery2(7) return ?

a)

0

b)

1

c)

4

d)

8

e)

13

81.

What value does mystery3(5,1) return ?

a)

1

b)

4

c)

5

d)

7

e)

8

82.

What value does mystery4(123,82) return ?

a)

1

b)

41

c)

82

d)

121

e)

242

83.

What value does mystery6(8) return ?

a)

0

b)

1

c)

7

d)

8

e)

9

84.

What is the main advantage of using recursion over iteration?

a)

Recursion is easier to debug than iteration.

b)

Recursion is always faster than iteration.

c)

Recursion can simplify code for problems that can be divided into similar subproblems.

d)

Recursion uses less memory than iteration.

85.

Which of the following is a characteristic of a recursive function?

a)

It can only be used for sorting algorithms.

b)

It must always return a value.

c)

It cannot call itself.

d)

It must have at least one base case.

86.

What is the result of calling a recursive function without a base case?

a)

The function will throw an exception.

b)

The function will terminate normally.

c)

The function will execute indefinitely until a stack overflow occurs.

d)

The function will return a value.