Recursion
Recursion
Consider the following recursive method, which is intended to display the binary equivalent of a decimal
number. For example, toBinary(100) should display 1100100.
Which of the following can replace /* missing code */ so that toBinary works as intended?
A
[Link](num % 2);
toBinary(num / 2);
B
[Link](num / 2);
toBinary(num % 2);
C
toBinary(num % 2);
[Link](num / 2);
D
toBinary(num / 2);
[Link](num % 2);
E
toBinary(num / 2);
[Link](num / 2);
2
Consider the following recursive method, which is intended to return a String with any
consecutive duplicate characters removed. For example, removeDupChars("aabcccd") returns
"abcd".
Which of the following can replace /* missing code */ so that removeDupChars works as intended?
A. return removeDupChars([Link](2));
B. return removeDupChars([Link](1)) + [Link](0, 1);
C. return removeDupChars([Link](2)) + [Link](1, 2);
D. return [Link](0, 1) + removeDupChars([Link](1));
E. return [Link](1, 2) + removeDupChars([Link](2));
3
Consider the following method, which is intended to return the sum of all the even digits in its
parameter num. For example, sumEvens(15555234) should return 6, the sum of 2 and 4.
Which of the following can be used as a replacement for /* missing statement */ so that the
sumEvens method works as intended?
Which of the following best describes the output produced by the method call mystery(val) ?
A. All integers from 1 to val, separated by spaces
B. All integers from val to 1, separated by spaces
C. The digits of val in their original order, separated by spaces
D. The digits of val in reverse order, separated by spaces
E. The last digit of val, then a space, then the first digit of
val
Which of the following best describes the result of the call doSomething(myString) ?
A. The method call returns a String containing the contents of myString unchanged.
B. The method call returns a String containing the contents of myString with the order of the
characters reversed from their order in myString.
C. The method call returns a String containing all but the first character of myString.
D. The method call returns a String containing only the first and second characters of myString.
E. The method call returns a String containing only the first and last characters of myString.
6
}
Which of the following best describes the value returned by the method call calc(num) ?
The following declaration and method call appear in a method in the same class as insertionSort.
How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to
insertionSort ?
A. 0
B. 1
C. 4
D. 5
E. 6
2
Consider the following correct implementation of the insertion sort algorithm.
public static void insertionSort(int[] elements)
{
for (int j = 1; j < [Link]; j++)
{
int temp = elements[j];
int possibleIndex = j;
while (possibleIndex > 0 && temp < elements[possibleIndex - 1])
{
elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex--;
}
elements[possibleIndex] = temp; // line 12
}
}
The following declaration and method call appear in a method in the same class as insertionSort.
int[] nums = {8, 7, 5, 4, 2, 1};
insertionSort(nums);
How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a
result of the call to insertionSort ?
A. 3
B. 4
C. 5
D. 6
E. 7
3
Consider the following correct implementation of the selection sort algorithm.
if (j != minIndex)
{
int temp = elements[j];
elements[j] = elements[minIndex];
elements[minIndex] = temp; // line 19
}
}
The following declaration and method call appear in a method in the same class as selectionSort.
How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result
of the call to selectionSort ?
A. 1
B. 2
C. 3
D. 4
E. 5
4
public static void mergeSortHelper(int[] arr, int from, int to, int[]
temp)
{
if (from < to)
{
int middle = (from + to) / 2;
mergeSortHelper(arr, from, middle, temp);
mergeSortHelper(arr, middle + 1, to, temp);
merge(arr, from, middle, to, temp);
}
}
The merge method is used to merge two halves of an array (arr[from] through arr[middle],
inclusive, and arr[middle + 1] through arr[to], inclusive) when each half has already been sorted
into ascending order. For example, consider the array arr1, which contains the values {1, 3, 5, 7,
2, 4, 6, 8}. The lower half of arr1 is sorted in ascending order (elements arr1[0] through arr1[3],
or {1, 3, 5, 7}), as is the upper half of arr1 (elements arr1[4] through arr1[7], or {2, 4, 6, 8}).
The array will contain the values {1, 2, 3, 4, 5, 6, 7, 8} after the method call merge(arr1, 0, 3, 7,
temp). The array temp is a temporary array declared in the calling program.
Consider the following code segment, which appears in a method in the same class as
mergeSortHelper and merge.
Which of the following represents the arrays merged the first time the merge method is executed
as a result of the code segment above?
public static int binarySearch(int[] arr, int low, int high, int target)
{
if (target == arr[middle])
{
return middle;
}
else if (target < arr[middle])
{
return binarySearch(arr, low, middle - 1, target);
}
else
{
return binarySearch(arr, middle + 1, high, target);
}
The following code segment appears in a method in the same class as binarySearch.
If the first call to binarySearch is the call in the code segment above, with low = 0 and high = 4,
which, if any, of the following shows the values of low and high when binarySearch is called for
the third time?
A. low = 0, high = 1
B. low = 0, high = 2
C. low = 1, high = 1
D. low = 2, high = 1
E. The method returns to the calling code segment before the third call to binarySearch.
6
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.
What is the value of arrIndex after the code segment has been executed?
A. 4
B. 5
C. 6
D. 7
E. 10
7
The following code segment appears in a method in the same class as binarySearch.
Including the call to binarySearch in the last statement of the given code segment, how many
times will binarySearch be called before a value is returned?
A. 1
B. 2
C. 3
D. 4
E. 8
8
/** Returns an index in arr where the value str appears if str appears
* in arr between arr[left] and arr[right], inclusive; otherwise returns -1.
* Precondition: arr is sorted in ascending order.
* left >= 0, right < [Link], [Link] > 0
*/
public static int bSearch(String[] arr, int left, int right, String str)
{
}
The following code segment appears in a method in the same class as bSearch.
How many times will "right" be printed when the code segment is executed?
A. 1
B. 2
C. 3
D. 7
E. 8
9
/** Returns an index in nums where target appears if target appears in nums between
* nums[lo] and nums[hi], inclusive; otherwise, returns -1.
* Precondition: nums is sorted in ascending order.
* low >= 0, high < [Link], [Link] > 0
*/
public static int bSearch(int[] nums, int low, int high, int target)
{
The following code segment appears in a method in the same class as bSearch.
int target = 3;
int[] nums = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int targetIndex = bSearch(nums, 0, [Link] - 1, target);
How many times will bSearch be called as a result of executing the code segment above?
A. 1
B. 2
C. 3
D. 4
E. 5
10
Consider the following correct implementation of the insertion sort algorithm. The insertionSort method
correctly sorts the elements of ArrayList data into increasing order.
public static void insertionsort(ArrayList<Integer> data)
{
for (int j = 1; j < [Link](); j++)
{
int v = [Link](j);
int k = j;
while (k > 0 && v < [Link](k - 1))
{
[Link](k, [Link](k - 1)); /* Statement 1 */
k--;
}
[Link](k, v); /* Statement 2 */
/* End of outer loop */
}
}
Assume that insertionSort has been called with an ArrayList parameter that has been initialized with the
following integer objects.
[5, 2, 4, 1, 3, 6]
What will the contents of data be after three passes of the outside loop (i.e., when j == 3 at the point
indicated by /* End of outer loop */) ?
(A) [1, 2, 3, 4, 5, 6]
(B) [1, 2, 3, 5, 4, 6]
(C) [1, 2, 4, 5, 3, 6]
(D) [2, 4, 5, 1, 3, 6]
(E) [5, 2, 1, 3, 4, 6]
11
Consider the following correct implementation of the insertion sort algorithm. The insertionsort method
correctly sorts the elements of ArrayList data into increasing order.
public static void insertionsort(ArrayList<Integer> data)
{
for (int j = 1; j < [Link](); j++)
{
int v = [Link](j);
int k = j;
while (k > 0 && v < [Link](k - 1))
{
[Link](k, [Link](k - 1)); /* Statement 1 */
k--;
}
[Link](k, v); /* Statement 2 */
/* End of outer loop */
}
}
Assume that insertionsort is called with an ArrayList parameter that has been initialized with the following
integer objects.
[1, 2, 3, 4, 5, 6 ]
How many times will the statements indicated by /* Statement 1 */ and /* Statement 2 */ execute?
(A)
Statement 1 Statement 2
0 0
(B)
Statement 1 Statement 2
0 5
(C)
Statement 1 Statement 2
0 6
(D)
Statement 1 Statement 2
5 5
(E)
Statement 1 Statement 2
6 6
12
Consider the following correct implementation of the selection sort algorithm.
public static void selectionSort(int[] elements)
{
for (int j = 0; j < [Link] - 1; j++)
{
int minindex = j;
for (int k = j + 1; k < [Link]; k++)
{
if (elements[k] < elements[minindex])
{
minindex = k;
}
}
if (j != minindex)
{
int temp = elements[j];
elements[j] = elements[minindex];
elements[minindex] = temp; // Line 19
}
}
}
The following declaration and method call appear in a method in the same class as selectionsort.
int[ ] arr = {9, 8, 7, 6, 5};
selectionSort(arr);
How many times is the statement elements [minindex ] = temp; in line 19 of the method
executed as a result of the call to selectionSort ?
(A) 1
(B) 2
(C) 3
(D) 4
(E) 5
13
Consider the following code segment from an insertion sort program.
for (int j = 1; j < [Link]; j++)
{
int insertitem = arr[j];
int k = j - 1;
while (k >= 0 && insertitem < arr[k])
{
arr[k + 1] = arr[k];
k--;
}
arr[k + 1] = insertitem;
/* end of for loop */
}
Assume that array arr has been defined and initialized with the values {5,4,3,2,1}. What are the
values in array arr after two passes of the for loop (i.e., when j = 2 at the point indicated by / * end of for
loop */) ?
(A) {2, 3, 4, 5, 1}
(B) {3, 2, 1, 4, 5}
(C) {3, 4, 5, 2, 1}
(D) {3, 5, 2, 3, 1}
(E) {5, 3, 4, 2, 1}
14
The following sort method correctly sorts the integers in elements into ascending order
Line 1: public static void sort (int [] elements)
Line 2: {
Line 3: for (int j = 0; j < elements. length - 1; j+ + )
Line 4: {
Line 5. int index = j ;
Line 6:
Line 7: for (int k = j + 1; k < elements. length; k+ + )
Line 8: {
Line 9: if (elements [k] < elements [index])
Line 10: {
Line 11: index = k;
Line 12: }
Line 13: }
Line 14:
Line 15: int temp = elements [ j ];
Line 16: elements [ j ] = elements [index];
Line 17: elements [index] = temp;
Line 18: }
Line 19: }
Which of the following changes to the sort method would correctly sort the integers in elements into
descending order?
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II and III
15
Consider the following correct implementation of the insertion sort algorithm.
public static void insertionSort(int[] elements)
{
for (int j= 1; j < [Link]; j++)
{
int temp = elements [j];
int possibleIndex = j;
The following declaration and method call appear in a method in the same class as insertionSort.
How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the
call to insertionSort?
(A) 2
(B) 3
(C) 4
(D) 5
(E) 6
16
Consider the following correct implementation of the selection sort algorithm.
public static void selectionSort(int[] elements)
{
The following declaration and method call appear in the same class as selectionsort.
How many times is the statement minIndex = k; in line 11 of the method executed as a result of the call
to selectionsort?
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
17
Consider the following two data structures for storing several million words.
Which of the following statements most accurately describes the time needed for operations on these data
structures?
Which of the following changes should be made so that selectSort will work as intended?
What is the intermediate value of arr after two iterations of the outer for loop in the call sort (arr)?
(A)
"Ann" "Mike" "Walt" "Lisa" "Shari" "Jose" "Mary" "Bill"
(B)
"Ann" "Mike" "Lisa" "Shari" "Jose" "Mary" "Bill" "Walt"
(C)
"Ann" "Bill" "Jose" "Lisa" "Mary" "Mike" "Shari" "Walt"
(D)
"Ann" "Mike" "Bill" "Lisa" "Mary" "Jose" "Shari" "Walt"
(E)
"Walt" "Shari" "Ann" "Lisa" "Mike" "Jose" "Mary" "Bill"
20
Directions: Select the choice that best fits each statement. The following question(s) refer to the following
information.
Consider the following sort method. This method correctly sorts the elements of array data into increasing
order.
public static void sort(int[]data)
{
for (int j=0; j<[Link] – 1; j ++)
{
int m = j;
for (int k = j+1;k < [Link]; k ++)
{
if (data[k] < data[m]) /*Compare values */
{
m = k;
}
}
int temp = data[m]; /*Assign to temp */
data[m] = data[j];
data[j]=temp;
/* End of outer loop */
}
}
Assume that sort is called with the array {6,3,2,5,4,1}.What will the value of data be after three passes of the
outer loop(i.e.,when j=2 at the point indicated by /* End of outer loop*/)?
(A) {1, 2, 3, 4, 5, 6}
(B) {1, 2, 3, 5, 4, 6}
(C) {1, 2, 3, 6, 5, 4}
(D) {1, 3, 2, 4, 5, 6}
(E) {1, 3, 2, 5, 4, 6}
21
Directions: Select the choice that best fits each statement. The following question(s) refer to the following
information.
Consider the following sort method. This method correctly sorts the elements of array data into increasing
order.
public static void sort(int[] data)
{
for (int j = 0; j < [Link] - 1; j++)
{
int m = j;
for (int k = j + 1; k < [Link]; k++)
{
if (data [k] < data [m] ) /* Compare values */
{
m = k;
}
}
int temp = data[m] ; /* Assign to temp */
data[m] = data[j];
data[j] = temp;
/ * End of outer loop * /
}
}
Assume that sort is called with the array {1,2,3,4,5,6}. How many times will the expression indicated by /*
Compare values */ and the statement indicated by /* Assign to temp */ execute?
(A) Compare values / Assign to temp
15/0
(B) Compare values / Assign to temp
15/5
(C) Compare values / Assign to temp
15/6
(D) Compare values / Assign to temp
21/5
(E) Compare values / Assign to temp
21/6
22
The following incomplete method is intended to sort its array parameter arr in increasing order.
//postcondition: arr is sorted in increasing order
public static void sortArray(int[] arr)
{
int j, k;
for (j = [Link] - 1; j > 0; j--)
{
int pos = j;
for ( /* missing code */ )
{
if (arr(k] > arr(pos])
{
pos = k;
}
}
swap(arr, j, pos);
}
}
Assume the swap(arr, j, pos) exchanges the values of arr[i] and arr[j]. Which of the
following could be used to replace /* missing code */ so that executing the code segment sorts the
values in array arr?