WorksheetsUnit 10 Recursion Review
Total questions: 20
Worksheet time: 2hrs 40mins
What is the action of method mystery2?
a+b
a*b
ab
ba
a!
COMPSC
COMPS
COMP
COM
CO
C
OMPSCI
MPSCI
PSCI
SCI
CI
I
COM
COMP
COMPS
COMPSC
COMPSCI
CO
COM
COMP
COMPS
COMPSC
COMPSCI
Why would you use a base case?
So that the code loops forever
So that the code does not loop forever
Because coding
What is returned by the call mystery(3,3)?
5
6
7
Stack Overflow
Which call to mystery would cause infinite recursion?
mystery(1,2)
mystery(2,2)
mystery(3,2)
none of these would cause infinite recursion
What is the output of this program?
2
3
4
ArrayIndexOutOfBoundsException
What is the output of this program?
2
3
4
ArrayIndexOutOfBoundsException
What would happen if the base case if not defined in the recursive method?
Stack Overflow
Stack Underflow
Program Crashes
None of the above
What is recursion in Java?
a class
a process of defining a method that calls other methods repeatedly
a process of defining a method that calls itself repeatedly
a process of defining a method that calls other methods which in turn call again this method
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));
today
todayto
todayoday
todayodayay
todayodaydayayy
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) ?
-1
3
5
6
8
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) ?
0
9
16
24
25
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?
StackOverFlowException
5
1
30
6
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?
001
100
1001
101
1110
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
I only
II only
III only
I and II
II and III
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
I only
II only
III only
I and II
I and III
use of the divide and conquer approach to arrange array elements
base case
merge sort
indirect recursion
void recursive method
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?
2
5
-1
2
5
2
2
6
-1
3
6
-1
-1
-1
2
