Chap 10
BMO the robot is programming a new game called "Open the box!" You give him a number and he tries to open a numbered box. He's using Binary Search to accomplish this. Unfortunately, the boxes are not sorted. They are in the following order: 1 3 6 9 14 10 21 Which box can NEVER be found using binary search?
14
Given this array: 1 2 4 5 6 7 8 12 14 21 22 42 53 How many comparisons are required to find 42 using the Binary Search?
3
Given the following recursive method public static int sumDigits(int number) { if (number == 0) { return 0; } int thisDigit = number%10; return thisDigit + sumDigits(number / 10); } Including the initial call, how many times will the sumDigits method be called if the initial call is sumDigits(4734)?
5
Consider the following recursive function: public static void printDigits(int number) { if (number == 0) { return; } System.out.println(number % 10); printDigits(number / 10); } What will be printed if printDigits(12345) is called?
5 4 3 2 1
Consider the following recursive function: public int mystery(int n) { if(n == 0) { return 2; } else { return 2 * mystery(n - 1); } } Java How many times will the function mystery be called if we call mystery(5) (be sure to include the first call mystery(5))
6
What is the largest number of comparisons needed to perform a binary search on an array with 42 elements?
6
The following recursive method is designed to adjust letter grades for students receiving a B, C, or D. public String adjustGrades(String grades) { if (grades.length() == 0) { return ""; } String thisGrade = grades.substring(0,1); if (thisGrade.equals("B")) { thisGrade = "A"; } else if (thisGrade.equals("C")) { thisGrade = "B"; } else if (thisGrade.equals("D")) { thisGrade = "C"; } return thisGrade + adjustGrades(grades.substring(1)); } Including the original call, how many times will the method be called using the following line of code: adjustGrades("AACDBA");
7
Given the following recursive method public static int sumDigits(int number) { if (number == 0) { return 0; } int thisDigit = number%10; return thisDigit + sumDigits(number / 10); } Assuming number is a properly initialized and defined variable, which of the following loops will produce the same results? I. int sum = 0; while (number > 0) { sum += number % 10; number /= 10; } II. int sum = 0; String num = "" + number; for (int i = 0; i <= num.length(); i++) { sum += number % 10; number /= 10; }
Both I and II
Which sorting method is implemented below? public int[] array; public int[] tempArr; public int length; public void sort(int[] inputArr) { array = inputArr; length = inputArr.length; tempArr = new int[length]; doSort(0, length - 1); } private void doSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; doSort(lowerIndex, middle); doSort(middle + 1, higherIndex); doSomething(lowerIndex, middle, higherIndex); } } private void doSomething(int lowerIndex, int middle, int higherIndex) { for (int i = lowerIndex; i <= higherIndex; i++) { tempArr[i] = array[i]; } int i = lowerIndex; int j = middle + 1; int k = lowerIndex; while (i <= middle && j <= higherIndex) { if (tempArr[i] <= tempArr[j]) { array[k] = tempArr[i]; i++; } else { array[k] = tempArr[j]; j++; } k++; } while (i <= middle) { array[k] = tempArr[i]; k++; i++; } }
Mergesort
The following recursive method is intended to return the number of occurrences of a word from a phrase. 1: public static int countWord(String phrase, String word) 2: { 3: if (phrase.indexOf(word) < 0) 4: { 5: return 0; 6: } 7: return 1 + countWord(phrase.substring(phrase.indexOf(word)), word); 8: } Which of the following best describes why this method does not work as intended?
The first parameter of the recursive call should be phrase.substring(phrase.indexOf(word)+ 1)
What approach does MergeSort use?
divide and conquer
We are searching for an int key in a sorted int array that has n elements. Under what circumstances will Linear Search / Sequential Search be more efficient than Binary Search?
key is the first element in the array
Given the following code: String number = "8735"; int sum = 0; for (int i = 0; i < number.length(); i++ ) { sum += Integer.parseInt(number.substring(i, i + 1)); } Which of the following recursive methods will produce the same result for mystery("8735")?
public static int mystery(String number) { if (number.length() == 0) { return 0; } int value = Integer.parseInt(number.substring(0, 1)); return value + mystery(number.substring(1)); }
What is the correct pseudocode for Insertion Sort?
For each element in the collection While there is a next element Compare the next unsorted element with the sorted elements Insert the element into the correct position in the sorted elements
How are Selection Sort and Insertion Sort different? I - Selection Sort is always faster than Insertion Sort II - Insertion Sort is always faster than Selection Sort III - When Selection Sort places an element into the sorted part of the array, that element is in its final position, whereas Insertion Sort may move the element later if it finds a smaller element. Selection Sort builds up an absolutely sorted array as it goes while Insertion Sort builds up a relatively sorted array as it goes. IV - Insertion Sort is faster if the array is already sorted or close to sorted
III and IV