Unit 10 Progress Check: MCQ

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following recursive method. public static String doSomething(String str) { if (str.length() < 1) { return ""; } else { return str.substring(0, 1) + doSomething(str.substring(1)); } } 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.

Consider the following method. /** Precondition: n > 0 */ public static void mystery(int n) { System.out.print(n + " "); if (n > 1) { mystery(n - 1); } } Which of the following best describes the output produced by the method call mystery(val) ?

B) All integers from val to 1, separated by spaces

Consider the following recursive method. /** Precondition: n > 0 */ public static int calc(int n) { if (n <= 9) { return n; } else { return calc(n / 10); } } Which of the following best describes the value returned by the method call calc(num) ?

B) The leftmost digit of num

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[] arr1 = {9, 1, 3, 5, 4}; int[] temp = new int[arr1.length]; mergeSortHelper(arr1, 0, arr1.length - 1, temp); Which of the following represents the arrays merged the first time the merge method is executed as a result of the code segment above?

A) {9} and {1} are merged to form {1, 9}.

Consider the following recursive method, which is intended to display the binary equivalent of a decimal number. For example, toBinary(100) should display 1100100. public static void toBinary(int num) { if (num < 2) { System.out.print(num); } else { /* missing code */ } } Which of the following can replace /* missing code */ so that toBinary works as intended?

D) toBinary(num / 2); System.out.print(num % 2);

Consider the following method, which implements a recursive binary search. /** Returns an index in arr where target appears, if target appears * in arr between arr[low] and arr[high], inclusive; * otherwise, returns -1. * Precondition: arr is sorted in ascending order. * low >= 0, high < arr.length, arr.length > 0 */ public static int binarySearch(int[] arr, int low, int high, int target) { if (low > high) { return -1; } int middle = (low + high) / 2; 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. int[] arr = {2, 3, 12, 34, 54}; int result = binarySearch(arr, 0, arr.length - 1, 5); 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?

C) low = 1, high = 1

Consider the following method, which implements a recursive binary search. /** 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. * lo >= 0, hi < nums.length, nums.length > 0 */ public static int bSearch(int[] nums, int lo, int hi, int target) { if (hi >= lo) { int mid = (lo + hi) / 2; if (nums[mid] == target) { return mid; } if (nums[mid] > target) { return bSearch(nums, lo, mid - 1, target); } else { return bSearch(nums, mid + 1, hi, target); } } return -1; } 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, nums.length - 1, target); How many times will bSearch be called as a result of executing the code segment above?

D) 4

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 target = 10; int[] arrWithDups = {2, 3, 7, 8, 10, 10, 10, 20}; int arrIndex = bSearch(arrWithDups, 0, arrWithDups.length - 1, target); What is the value of arrIndex after the code segment has been executed?

B) 5

Consider the following method, which implements a recursive binary search. /** 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 < arr.length, arr.length > 0 */ public static int bSearch(String[] arr, int left, int right, String str) { if (right >= left) { int mid = (left + right) / 2; if (arr[mid].equals(str)) { return mid; } else if (arr[mid].compareTo(str) > 0) { return bSearch(arr, left, mid - 1, str); } else { System.out.println("right"); return bSearch(arr, mid + 1, right, str); } } return -1; } The following code segment appears in a method in the same class as bSearch. String[] words = {"arc", "bat", "cat", "dog", "egg", "fit", "gap", "hat"}; int index = bSearch(words, 0, words.length - 1, "hat"); How many times will "right" be printed when the code segment is executed?

C) 3

Consider the following method, which implements a recursive binary search. /** Returns an index in theList where target appears, * if target appears in theList between the elements at indices * low and high, inclusive; otherwise returns -1. * Precondition: theList is sorted in ascending order. * low >= 0, high < theList.size(), theList.size() > 0 */ public static int binarySearch(ArrayList<Integer> theList, int low, int high, int target) { if (low > high) { return -1; } int middle = (low + high) / 2; if (target == theList.get(middle)) { return middle; } else if (target < theList.get(middle)) { return binarySearch(theList, low, middle - 1, target); } else { return binarySearch(theList, middle + 1, high, target); } } The following code segment appears in a method in the same class as binarySearch. ArrayList<Integer> theList = new ArrayList<Integer>(); for (int k = 10; k < 65; k = k + 5) { theList.add(k); } int result = binarySearch(theList, 0, theList.size() - 1, 45); 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?

D) 4

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. /** Precondition: num >= 0 */ public static int sumEvens(int num) { if (num < 10 && num % 2 == 0) { return num; } else if (num < 10) { return 0; } else if (num >= 10 && num % 2 == 0) { /* missing statement */ } else { return sumEvens(num / 10); } } Which of the following can be used as a replacement for /* missing statement */ so that the sumEvens method works as intended?

D) return num % 10 + sumEvens(num / 10);

Consider the following recursive method, which is intended to return a String with any consecutive duplicate characters removed. For example, removeDupChars("aabcccd") returns "abcd". public static String removeDupChars(String str) { if (str == null || str.length() <= 1) { return str; } else if (str.substring(0, 1).equals(str.substring(1, 2))) { return removeDupChars(str.substring(1)); } else { /* missing code */ } } Which of the following can replace /* missing code */ so that removeDupChars works as intended?

D) return str.substring(0, 1) + removeDupChars(str.substring(1));


Conjuntos de estudio relacionados

Quiz 9: Judgement, Decisions, and Reasoning

View Set

Discovering Psychology Chapter 4,5,& 6.

View Set

Cold War and African and Indian Decolonization Study Guide FOR FINAL

View Set

Chapter 4: Alkanes and Cycloalkanes

View Set

NEW VEHICLE DETAIL, STORAGE AND DELIVERY

View Set