ch 10(1)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

6

Consider the following recursive method. /** Precondition: 0 <= numVals <= nums.length */ public static int mystery(int[] nums, int v, int numVals) { if (numVals == 0) { return 0; } else if (v == nums[numVals - 1]) { return 1 + mystery(nums, v, numVals - 1); } else { return mystery(nums, v, numVals - 1); } } Which of the following best describes the value returned by the call mystery(nums, v, nums.length) ? A The value 0 is returned. B The value 1 is returned. C The number of times that v occurs in nums is returned. D The number of times that numVals occurs in nums is returned. E Nothing is returned. A runtime error occurs because of infinite recursion.

The number of times that v occurs in nums is returned

Consider the following recursive method. public static boolean recurMethod(String str) { if (str.length() <= 1) { return true; } else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0) { return recurMethod(str.substring(1)); } else { return false; } } Which of the following method calls will return true ? A recurMethod("abcba") B recurMethod("abcde") C recurMethod("bcdab") D recurMethod("edcba") E recurMethod("edcde")

recurMethod("edcba")

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 a

{30, 50, 80} and {20, 60, 70} are merged to form {20, 30, 50, 60, 70, 80}.

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information Consider the following binarySearch method. The method correctly performs a binary search. Consider the following code segment. int [ ] values = {1, 2, 3, 4, 5, 8, 8, 8};int target = 8; What value is returned by the call binarySearch (values, target) ? A -1 B 3 C 5 D 6 E 8

5

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information Consider the following binarySearch method. The method correctly performs a binary search. Suppose the binarySearch method is called with an array containing 2,000 elements sorted in increasing order. What is the maximum number of times that the statement indicated by / * Calculate midpoint * / could execute? A 2,000 B 1,000 C 20 D 11 E 1

11

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

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

2

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 a

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

4

Consider the following method. // precondition: arr contains no duplicates; // the elements in arr are in sorted order; // 0 ≤ low ≤ arr.length; low - 1 ≤ high < arr.length public static int mystery(int[] arr, int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(arr, mid + 1, high, num); } else if (arr[mid] > num) { return mystery(arr, low, mid - 1, num); } else // arr{mid] == num { return mid; } } How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1, 14) if arr is the following array? A 1 B 2 C 4 D 7 E 8

4

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information Consider the following instance variable and methods. You may assume that data has been initialized with length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such element exists. For which of the following test cases will the call seqSearchRec(5) always result in an error? data contains only one element. data does not contain the value 5. data contains the value 5 multiple times. A I only B II only C III only D I and II only E I, II, and III

II only


संबंधित स्टडी सेट्स

Module 5.3: Credit Card Statement

View Set

Microbiology (Bio 261) Chapter 10

View Set

Final Multiple Choice Study Guide

View Set

Seleccionar Audio Escucha el audio para escoger la opción correcta para completar cada oración.

View Set

Umass Amherst - Legal Studies 250 Midterm

View Set