Recursive Searching and Sorting MCQ Practice

Question 1 of 10

A harbor office stores buoy codes. What text does this Java code display?

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

System.out.print(flip("buoy"));
  1. A. buoy
  2. B. youb
  3. C. yobu
  4. D. uboy

Question 2 of 10

A festival booth stores lantern wattages in an ArrayList. What text does this Java code display?

public static int watts(ArrayList<Integer> lamps, int index)
{
    if (index == lamps.size())
    {
        return 0;
    }
    return lamps.get(index) + watts(lamps, index + 1);
}

ArrayList<Integer> lamps = new ArrayList<Integer>();
lamps.add(4);
lamps.add(9);
lamps.add(2);
lamps.add(6);
System.out.print(watts(lamps, 0));
  1. A. 17
  2. B. 15
  3. C. 432
  4. D. 21

Question 3 of 10

A museum catalog shortens exhibit codes. What text does this Java code display?

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

System.out.print(keep("MURAL"));
  1. A. MRL
  2. B. UAL
  3. C. MURA
  4. D. ML

Question 4 of 10

A greenhouse clerk checks whether a tray label reads the same forward and backward. What text does this Java code display?

public static boolean mirror(String s)
{
    if (s.length() <= 1)
    {
        return true;
    }
    if (!s.substring(0, 1).equals(s.substring(s.length() - 1)))
    {
        return false;
    }
    return mirror(s.substring(1, s.length() - 1));
}

System.out.print(mirror("level") + " " + mirror("leaf"));
  1. A. true true
  2. B. false false
  3. C. true false
  4. D. false true

Question 5 of 10

A rec center stores locker numbers in increasing order. What value does hunt(lockers, 40, 0, 6) return?

Index 0 1 2 3 4 5 6
Value 12 18 25 31 40 47 55
public static int hunt(int[] data, int target, int low, int high)
{
    if (low > high)
    {
        return -1;
    }
    int mid = (low + high) / 2;
    if (data[mid] == target)
    {
        return mid;
    }
    else if (data[mid] < target)
    {
        return hunt(data, target, mid + 1, high);
    }
    else
    {
        return hunt(data, target, low, mid - 1);
    }
}
  1. A. 31
  2. B. 4
  3. C. 40
  4. D. -1

Question 6 of 10

The same hunt method searches the locker array for 26. How many times is mid computed before the method returns?

Call low high mid data[mid] Next range
1 0 6 3 31 0 through 2
2 0 2 1 18 2 through 2
3 2 2 2 25 3 through 2
  1. A. 1
  2. B. 2
  3. C. 7
  4. D. 3

Question 7 of 10

A dock board lists ferry wait times that are not in order: 9, 4, 18, 12, 7. A volunteer runs hunt(times, 12, 0, 4) using the same method as the locker search. Which statement is true?

  1. A. The method returns 3 because 12 is stored at index 3
  2. B. The method returns 2 because the first middle value is 18
  3. C. The method may return -1 even though 12 is present, because the values are not sorted
  4. D. The method does not compile because binary search cannot be recursive

Question 8 of 10

A badge drawer starts with these IDs. mergeSort(ids, 0, 3) runs. After the first split, which pair of subarrays is sorted recursively?

Index 0 1 2 3
Value 44 11 30 8
public static void mergeSort(int[] data, int from, int to)
{
    if (from < to)
    {
        int mid = (from + to) / 2;
        mergeSort(data, from, mid);
        mergeSort(data, mid + 1, to);
        merge(data, from, mid, to);
    }
}
  1. A. [44, 11] and [30, 8]
  2. B. [44] and [11, 30, 8]
  3. C. [44, 11, 30] and [8]
  4. D. [8, 11, 30, 44]

Question 9 of 10

The same mergeSort continues until every piece of [44, 11, 30, 8] has one element. After each half merges its two one-element lists, which sorted halves are ready for the final merge?

  1. A. [44, 11] and [30, 8]
  2. B. [8, 11, 30, 44]
  3. C. [11, 44] and [8, 30]
  4. D. [44, 30] and [11, 8]

Question 10 of 10

A clerk merges two already-sorted badge halves using this rule: repeatedly take the smaller of the two front values; if they are equal, take the left value. What is the merged result of [2, 9, 15] and [4, 11]?

  1. A. [2, 9, 15, 4, 11]
  2. B. [2, 4, 9, 11, 15]
  3. C. [2, 4, 9, 15, 11]
  4. D. [4, 11, 2, 9, 15]