Comp sci

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following implementation of the insertion sort algorithm. public static void insertionSort(ArrayList<Integer> nums) { for (int i = 1; i < nums.size(); i ++){ int temp = nums.get(i); int index = i; while (index >= 0 && temp > nums.get(index - 1)) { nums.set(index, nums.get(index-1)); index --; } nums.set(index, temp); } } An ArrayList containing the values [11, 8, 12, 7, 21, 15, 1] is sorted using the above method. What is stored in the ArrayList after the for loop has executed twice? A. [8, 11, 12, 7, 21, 15, 1] B. [11, 8, 12, 7, 1, 15, 21] C. [12, 11, 8, 7, 21, 15, 1] D. [11, 8, 12, 7, 21, 15, 1] E. [7, 8, 11, 12, 21, 15, 1]

A

Consider the following method that is intended to return true if an array of integers is arranged in increasing order and return false otherwise. /** * @param nums an array of integers * @return true if the values in the array appear in increasing order, * false otherwise */ public static boolean isIncreasing(int[] nums) { /* missing code */ } Which of the following can be used to replace /* missing code */ so that isIncreasing will work as intended? I. for (int k = 0; k < nums.length - 1; k++) { if (nums[k] > nums[k + 1]) { return false; } } return true; II. for (int k = 0; k < nums.length - 1; k++) { if (nums[k] < nums[k + 1]) { return true; } } return false; III. for (int k = 0; k < nums.length - 1; k++) { if (nums[k] >= nums[k + 1]) { return false; } else { return true; } } return true; A. I only B. II only C. III only D. I and II E. I and III

A

Consider the following method, which implements a recursive binary search that returns an index in myListwhere target appears, if target appears in myList between the elements at indices low and high, inclusive; otherwise returns -1. public static int bSearch(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, 1, 1, 2, 3, 5, 8, 13, 21, 34] What value will be returned by the call bSearch(inputList, 0, 9, 55)? A. -1 B. 1 C. 3 D. 5 E. 9

A

The following incomplete method is intended to sort its array parameter arr in increasing order. // Postcondition: arr is sorted in increasing order public static void sortArray(int[] arr1) { int j, k; for (j = arr1.length - 1; j > 0; j--) { int pos = j; for ( /* missing code */ ) { if (arr1[k] > arr[pos]) { pos = k; } } swap(arr1, j, pos); } } Assume that swap(arr1, j, pos) exchanges the values of arr1[j] and arr1[pos]. Which of the following could be used to replace /* missing code */ so that executing the code segment sorts the values in array arr1? A. k = j - 1; k >= 0; k-- B. k = j - 1; k > 0; k-- C. k = 1; k < arr1.length; k++ D. k = 1; k > arr1.length; k++ E. k = 0; k <= arr1.length; k++

A

Consider the following instance variable and method. private int[] numbers; public static int mystery(int num) { for (int k = 0; k < numbers.length; k++) { if (numbers[k] > num) { return k; } } return numbers.length; } Which of the following best describes the contents of numbers after the following statement has been executed? int m = mystery(n); A. The array is in sorted order up to position k. B. The smallest value is at position k. C. The largest value is at position k. D. All values in position 0 to m-1 are greater than num. E. All values in position 0 to m-1 are less than or equal to num.

E

uppose the binarySearch method is called with an array containing 20 elements sorted in increasing order. What is the maximum number of times that the statement indicated by /* calculate midpoint */ could execute? public static int binarySearch(int[] a, int target) { int left = 0; int right = a.length - 1; while (left <= right) { int mid = (left + right) / 2; /* calculate midpoint */ if (a[mid] < target) { left = mid + 1; } else if (a[mid] > target) { right = mid - 1; } else { return mid; } } return -1; } A. 21 B. 20 C. 19 D. 10 E. 5

E

Consider the following implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // Line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] ss = {10, 9, 8, 7, 6}; selectionSort(ss); How many times is the statement elements[minIndex] = temp; in Line 19 of the method executed as a result of the call to selectionSort? A. 1 B. 2 C. 3 D. 4 E. 5

B

Consider the following method, which implements a recursive binary search that 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. 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 = {20, 30, 30, 60, 70, 90}; int result = bSearch(nums, 0, nums.length - 1, 30); What is the value of result after the code segment has been executed? A. 1 B. 2 C. 3 D. 4 E. 5

B

Which of the following are NOT true of insertion and selection sort? A. When an element is moved to the sorted list in insertion sort, it may move again if a smaller item is found. B. Selection sort is generally slower than insertion sort on a randomized list. C. When an element is moved in selection sort, it is put into a sorted position and will not move again. D. Insertion sort is generally faster for a nearly sorted array. E. Selection sort is generally faster for a reverse sorted array.

B

2. Consider the following code segment. int[] values = {1, 1, 1, 2, 2, 3}; int target = 1; What value is returned by the call binarySearch(values, target)? public static int binarySearch(int[] a, int target) { int left = 0; int right = a.length - 1; while (left <= right) { int mid = (left + right) / 2; if (a[mid] < target) { left = mid + 1; } else if (a[mid] > target) { right = mid - 1; } else { return mid; } } return -1; } A. 0 B. 1 C. 2 D. 3 E. 4

C

Consider the following two data structures for storing millions of words. I. An array of words, not in any particular order II. An array of words, sorted in alphabetical order Which of the following statements most accurately describes the time needed for operations on these data structures? A. Inserting a word is faster in II than in I. B. Finding a given word is faster in I than in II. C. Finding a given word is faster in II than in I. D. Finding the longest word is faster in II than in I. E. Finding the first word in alphabetical order is faster in I than in II.

C

Consider the following code segment. public static int getLocation(int[] arr, int findMe) { for (int i = 0; i < arr.length; i++) { if (arr[i] == findMe) { // check return i; } } return -1; } int[] nums = {11, 28, 5, 25, 86}; int location = getLocation(nums, 86); How many times does the line labeled // check get executed? A. 0 B. 1 C. 4 D. 5 E. 6

D

Consider the following instance variable and method. private int[] values; /** * Precondition: values.length > 0 */ public int checkArray() { int loc = 0; for (int k = 1; k < values.length; k++) { if (values[k] < values[loc]) { loc = k; } } return loc; } Which of the following is the best postcondition for checkArray? A. Returns the largest value in the array values. B. Returns the smallest value in the array values. C. Returns the index of the largest value in the array values. D. Returns the index of the smallest value in the array values. E. Returns 0.

D

Consider the following method. public static String mystery(String[] arr, String[] arr2) { for (String s : arr) { boolean check = true; for (String c : arr2) { if (s.indexOf(c) < 0) { check = false; } } if (check) { return s; } } return null; } What would the following code segment print? String[] s = {"night", "evening", "afternoon", "morning"}; String[] t = {"ni", "rn"}; System.out.println(mystery(s, t)); A. night B. evening C. afternoon D. morning E. null

D

The following method is intended to return the index of the location of target in the ArrayList arrList, otherwise returns -1. public int find(ArrayList<Integer> arrList, int target) { for (int i = 0; i < arrList.size(); i++) { if (target == arrList.get(i)) { return i; } return -1; } return -1; } Which of the following statements is true about the method? I. It returns the intended output if target is the first item in arrList. II. It returns the intended output if target is the last item in arrList. III. It returns the intended output if target is not in arrList. A. I only B. II only C. III only D. I and III E. II and III

D


Ensembles d'études connexes

Chapter 5 Semester Test Study Guide - Intro. to Criminal Justice

View Set

NYS Life and Health Pre License: XCEL ch 1

View Set

Combo with "drivechapta30" and 14 others

View Set

Chapter 33: THE BUILDING OF GLOBAL EMPIRES

View Set

The Early Republic Lesson 7 Jacksonian Democracy

View Set

Cap's AP Psych Chapter 1 - Test (Ex 4)

View Set