Looks like no one added any tags here yet for you.
Consider the following method.
public int addFun(int n)
{
if (n <= 0)
return 0;
if (n == 1)
return 2;
return addFun(n - 1) + addFun(n - 2);
}
What value is returned as a result of the call addFun(6) ?
A
10
B
12
C
16
D
26
E
32
16
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
todayodayay
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
25
Consider the following recursive method.
Assuming that k is a nonnegative integer and m = 2^k, what value is returned as a result of the call mystery (m) ?
A
0
B
k
C
m
D
E
k
// precondition: x >= 0
public void mystery(int x)
{
if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}
Which of the following is printed as a result of the call mystery(123456) ?
A
16
B
56
C
123456
D
654321
E
Many digits are printed due to infinite recursion.
123456
Consider the following method, which implements a recursive binary search.
/** Returns an index in arr where the value x appears if x appears
* in arr between arr[left] and arr[right], inclusive;
* otherwise returns -1.
* Precondition: arr is sorted in ascending order.
* left >= 0, right < arr.length, arr.length > 0
*/
public static int bSearch(int[] arr, int left, int right, int x)
{
if (right >= left)
{
int mid = (left + right) / 2;
if (arr[mid] == x)
{
return mid;
}
else if (arr[mid] > x)
{
return bSearch(arr, left, mid - 1, x);
}
else
{
return bSearch(arr, mid + 1, right, x);
}
}
return -1;
}
The following code segment appears in a method in the same class as bSearch.
int[] nums = {10, 20, 30, 40, 50};
int result = bSearch(nums, 0, nums.length - 1, 40);
How many times will the bSearch method be called as a result of executing the code segment, including the initial call?
A
1
B
2
C
3
D
4
E
5
2
Consider the following recursive method.
public static void stars(int num)
{
if (num == 1)
{
return;
}
stars(num - 1);
for (int i = 0; i < num; i++)
{
System.out.print("*");
}
System.out.println();
}
What is printed as a result of the method call stars(5) ?
A
*****
B
**
***
****
*****
C
*
**
***
****
*****
D
*****
****
***
**
E
*****
****
***
**
*
**
***
****
*****
Consider the following recursive method.
What value is returned as a result of the call recur(27)?
A
8
B
9
C
12
D
16
E
18
16