Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Unit 10 Recursion Review

Total questions: 20

Worksheet time: 2hrs 40mins

Name
Class
Date
1.
What is the returned value of recMethod(5)?
a)
68
b)
70
c)
75
d)
82
2.

What is the action of method mystery2?

a)

a+b

b)

a*b

c)

ab

d)

ba

e)

a!

3.
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
4.

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

5.
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.
6.

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

a)

5

b)

6

c)

7

d)

Stack Overflow

7.

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

8.

What is the output of this program?

a)

2

b)

3

c)

4

d)

ArrayIndexOutOfBoundsException

9.

What is the output of this program?

a)

2

b)

3

c)

4

d)

ArrayIndexOutOfBoundsException

10.

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

11.

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

12.

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

13.

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

14.

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

15.

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

16.

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

17.

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

18.

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

19.

use of the divide and conquer approach to arrange array elements

a)

base case

b)

merge sort

c)

indirect recursion

d)

void recursive method

20.

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