compsci test 10
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 = {0, 4, 4, 5, 6, 7}; int result = bSearch(nums, 0, nums.length - 1, 4); What is the value of result after the code segment has been executed? A 1 B 2 C 3 D 4 E 5
B 2
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
B 2
The bark method below is intended to print the string "woof" a total of num times. public static void bark(int num) { if (num > 0) { System.out.println("woof"); /* missing code */ } } Which of the following can be used to replace /* missing code */ so that the call bark(5) will cause "woof" to be printed five times? A bark(num - 5); B bark(num - 1); C bark(num); D bark(num + 1); E bark(num + 5);
B bark(num - 1);
Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /** Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ 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. int[] numbers = {40, 10, 20, 30}; int[] temp = new int[numbers.length]; mergeSortHelper(numbers, 0, numbers.length - 1, temp); How many times will the merge method be called as a result of executing the code segment? A 1 B 2 C 3 D 4 E 5
C 3
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 statement appears in a method in the same class as bSearch. Assume that nums is a sorted array of length 7, containing only positive integers. int result = bSearch(nums, 0, nums.length - 1, -100); How many times will the bSearch method be called as a result of executing the statement, including the initial call? A 1 B 3 C 4 D 5 E 7
C 4
Consider the following method, which implements a recursive binary search. /** Returns an index in myList where target appears, * if target appears in myList between the elements at indices * low and high, inclusive; otherwise returns -1. * Precondition: myList is sorted in ascending order. * low >= 0, high < myList.size(), myList.size() > 0 */ public static int binarySearch(ArrayList<Integer> myList, int low, int high, int target) { int mid = (high + low) / 2; if (target < myList.get(mid)) { return binarySearch(myList, low, mid - 1, target); } else if (target > myList.get(mid)) { return binarySearch(myList, mid + 1, high, target); } else if (myList.get(mid).equals(target)) { return mid; } return -1; } Assume that inputList is an ArrayList of Integer objects that contains the following values. [0, 10, 30, 40, 50, 70, 70, 70, 70] What value will be returned by the call binarySearch(inputList, 0, 8, 70) ? A -1 B 5 C 6 D 7 E 8
C 6
Consider the following method. public static int mystery(ArrayList<Integer> numList) { if (numList.size() == 0) { return 0; } else { int val = numList.remove(0); return val + mystery(numList); } } Which of the following best describes the value returned by the method? A It returns the value of the first element in numList. B It returns the value of the last element in numList. C It returns the sum of the elements in numList. D It returns 0 regardless of the contents of numList. E It returns nothing due to infinite recursion.
C It returns the sum of the elements in numList.
Consider the following method, which is intended to return the largest value in the portion of the int array data that begins at the index start and goes to the end of the array. /** Precondition: 0 <= start < data.length */ public int maximum(int[] data, int start) { if (start == data.length - 1) { return data[start]; } /* missing statement */ if (val > data[start]) { return val; } else { return data[start]; } } Which of the following can be used as a replacement for /* missing statement */ so that the maximum method works as intended? A int val = maximum(data, start); B int val = maximum(data, start - 1); C int val = maximum(data, start + 1); D int val = maximum(data[start - 1], start); E int val = maximum(data[start + 1], start);
C int val = maximum(data, start + 1);
The printRightToLeft method is intended to print the elements in the ArrayList words in reverse order. For example, if words contains ["jelly bean", "jukebox", "jewelry"], the method should produce the following output. jewelry jukebox jelly bean The method is shown below. public static void printRightToLeft(ArrayList<String> words) { if (words.size() > 0) { System.out.println(words.get(words.size() - 1)); /* missing code */ } } Which of the following can be used to replace /* missing code */ so that the printRightToLeft method works as intended? A words.remove(0); printRightToLeft(words); B words.remove(words.size()); printRightToLeft(words); C words.remove(words.size() - 1); printRightToLeft(words); D printRightToLeft(words); words.remove(0); E printRightToLeft(words); words.remove(words.size() - 1);
C words.remove(words.size() - 1); printRightToLeft(words);
Consider the following method. public static void strChange(String str) { if (str.length() > 0) { strChange(str.substring(1)); System.out.print(str.substring(0, 1)); } } Which of the following best describes the behavior of the method? A It prints the first character of str. B It prints the last character of str. C It prints the characters of str in the order they appear. D It prints the characters of str in reverse order. E It prints nothing due to infinite recursion.
D It prints the characters of str in reverse order.
Consider the following method. /* Precondition: j <= k */ public static void mystery(int j, int k) { System.out.println(j); if (j < k) { mystery(j + 1, k); } } Which of the following best describes the behavior of the mystery method? A It repeatedly prints the value j due to infinite recursion. B It prints the initial value of j a total of k times. C It prints the initial value of k a total of j times. D It prints the integers from j to k, inclusive, in order from least to greatest. E It prints the integers from j to k, inclusive, in order from greatest to least.
D It prints the integers from j to k, inclusive, in order from least to greatest.
Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /** Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ 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. int[] vals = {80, 50, 30, 20, 60, 70}; int[] temp = new int[vals.length]; mergeSortHelper(vals, 0, vals.length - 1, temp); Which of the following represents the arrays merged the last time the merge method is executed as a result of the code segment above? A {20, 30, 50} and {60, 70, 80} are merged to form {20, 30, 50, 60, 70, 80}. B {20, 50, 70} and {30, 60, 80} are merged to form {20, 30, 50, 60, 70, 80}. C {20, 50, 70} and {30, 60, 80} are merged to form {20, 50, 70, 30, 60, 80}. D {30, 50, 80} and {20, 60, 70} are merged to form {20, 30, 50, 60, 70, 80}. E {30, 50, 80} and {20, 60, 70} are merged to form {30, 50, 80, 20, 60, 70}.
D {30, 50, 80} and {20, 60, 70} are merged to form {20, 30, 50, 60, 70, 80}.