Recursive Searching and Sorting MCQ Practice

Question 1 of 10

A bakery reprints icing codes from the last letter backward. What text does this Java code display?

public static String peel(String s)
{
    if (s.length() <= 1)
    {
        return s;
    }
    return s.substring(s.length() - 1) + peel(s.substring(0, s.length() - 1));
}

System.out.print(peel("cake"));
  1. A. cake
  2. B. acek
  3. C. ekac
  4. D. ecak

Question 2 of 10

A camp office stores bunk counts. What text does this Java code display?

public static int smallest(ArrayList<Integer> bunks, int index)
{
    if (index == bunks.size() - 1)
    {
        return bunks.get(index);
    }
    int later = smallest(bunks, index + 1);
    int here = bunks.get(index);
    if (here < later)
    {
        return here;
    }
    return later;
}

ArrayList<Integer> bunks = new ArrayList<Integer>();
bunks.add(8);
bunks.add(3);
bunks.add(11);
bunks.add(3);
bunks.add(9);
System.out.print(smallest(bunks, 0));
  1. A. 3
  2. B. 8
  3. C. 9
  4. D. 11

Question 3 of 10

A used-book stall keeps only the letter O from a shelf code. What text does this Java code display?

public static String onlyO(String s)
{
    if (s.length() == 0)
    {
        return "";
    }
    String first = s.substring(0, 1);
    String rest = onlyO(s.substring(1));
    if (first.equals("O"))
    {
        return first + rest;
    }
    return rest;
}

System.out.print(onlyO("LOOKBOOK"));
  1. A. LOOKBOOK
  2. B. LBK
  3. C. OO
  4. D. OOOO

Question 4 of 10

A recitals desk totals every other score, starting at index 0. What text does this Java code display?

public static int stride(int[] scores, int index)
{
    if (index >= scores.length)
    {
        return 0;
    }
    return scores[index] + stride(scores, index + 2);
}

int[] scores = {5, 8, 2, 9, 4};
System.out.print(stride(scores, 0));
  1. A. 28
  2. B. 11
  3. C. 17
  4. D. 4

Question 5 of 10

A harbor clerk stores ferry departure minutes in an ArrayList already sorted from smallest to largest. What value does hunt(times, 21, 0, 7) return?

Index 0 1 2 3 4 5 6 7
Value 6 9 14 21 28 35 42 49
public static int hunt(ArrayList<Integer> data, int target, int low, int high)
{
    if (low > high)
    {
        return -1;
    }
    int mid = (low + high) / 2;
    if (data.get(mid) == target)
    {
        return mid;
    }
    else if (data.get(mid) < target)
    {
        return hunt(data, target, mid + 1, high);
    }
    else
    {
        return hunt(data, target, low, mid - 1);
    }
}
  1. A. 3
  2. B. 21
  3. C. 0
  4. D. 4

Question 6 of 10

A seed shop runs the same recursive hunt on this sorted list, searching for 20. After the first comparison, what are low and high for the next call?

Index 0 1 2 3 4 5 6
Value 10 20 30 40 50 60 70
  1. A. low 0, high 6
  2. B. low 4, high 6
  3. C. low 0, high 2
  4. D. low 0, high 3

Question 7 of 10

Which statement about binary search on an array or ArrayList is true?

  1. A. The values may be unsorted, because each call still inspects the middle index
  2. B. The values must already be sorted, and each comparison typically discards about half of the remaining range
  3. C. Binary search always inspects every index, just as a linear scan does
  4. D. Binary search can be used only with String data, never with ArrayList

Question 8 of 10

A race table merge-sorts finish-line seconds with mergeSort(times, 0, 4) and mid = (from + to) / 2. After the first split, which pair of subarrays is sorted recursively?

Index 0 1 2 3 4
Value 9 3 7 1 5
  1. A. [9, 3] and [7, 1, 5]
  2. B. [9] and [3, 7, 1, 5]
  3. C. [9, 3, 7, 1] and [5]
  4. D. [9, 3, 7] and [1, 5]

Question 9 of 10

mergeSort is used on [16, 5, 12] with from 0 and to 2. After the left half has been divided to one-element lists and merged, and the right half is already a single remaining value, which pair is merged last?

  1. A. [5, 16] and [12]
  2. B. [16] and [5, 12]
  3. C. [16, 5, 12] treated as already sorted
  4. D. [12, 16] and [5]

Question 10 of 10

A lantern shop merges two already-sorted halves. While both halves still have unused values, the smaller front value is copied; if the two front values are equal, the left value is copied. What is the merged result of [3, 8, 10] and [3, 6, 12]?

  1. A. [3, 8, 10, 3, 6, 12]
  2. B. [3, 3, 8, 6, 10, 12]
  3. C. [3, 3, 6, 8, 10, 12]
  4. D. [12, 10, 8, 6, 3, 3]