ap csa unit 7
Consider the following code segment. ArrayList<String> arrList = new ArrayList<String>(); arrList.add("A"); arrList.add("B"); arrList.add("C"); arrList.add("D"); for (int i = 0; i < arrList.size(); i++) { System.out.print(arrList.remove(i)); } What, if anything, is printed as a result of executing the code segment? A AC B BD C ABC D ABCD E Nothing is printed.
Answer A Correct. During the first iteration of the loop, i has the value 0 and the size of arrList is 4, so the element at index 0 ("A") is removed and printed. During the second iteration of the loop, i has the value 1 and the size of arrList is 3, so the element at index 1 ("C") is removed and printed. During the third comparison of i and arrList.size() in the loop header, i has the value 2 and the size of arrList is 2, so the loop terminates.
In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements with the value 0 from myList. int j = 0; while (j < myList.size()) { if (myList.get(j) == 0) { myList.remove(j); } j++; } The code segment does not always work as intended. For which of the following lists does the code segment NOT produce the correct result? A {0, 1, 2, 3} B {0, 1, 0, 2} C {1, 0, 0, 2} D {1, 2, 3, 0} E {1, 2, 3, 4}
Answer C Correct. For this ArrayList, the code segment removes the element at index 1, shifting the second instance of 0 to index 1. The code segment then increases j to 2, so the second instance of 0 is never removed.
Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store Integer values. ArrayList<Integer> numbers = /* missing code */; Which of the following can be used to replace /* missing code */ so that the statement works as intended? I. new ArrayList() II. new ArrayList<Integer> III. new ArrayList<Integer>() A III only B I and II only C I and III only D II and III only E I, II, and III
Answer C Correct. Statement I correctly declares an ArrayList object that can contain Integer objects. Statement II results in a syntax error, as the call to the ArrayList constructor must contain (). Statement III correctly declares an ArrayList object that can contain elements only of type Integer.
Consider the following method definition. The method isReversed is intended to return true if firstList and secondList contain the same elements but in reverse order, and to return false otherwise. /** Precondition: firstList.size() == secondList.size() */ public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer> secondList) { for (int j = 0; j < firstList.size() / 2; j++) { if (firstList.get(j) != secondList.get(secondList.size() - 1 - j)) { return false; } } return true; } The method does not always work as intended. For which of the following inputs does the method NOT return the correct value? A When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1} B When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3} C When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1} D When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1} E When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}
Answer C Correct. The loop will iterate two times. Therefore, the 1 in firstList will be compared with the 1 in secondList and the 3 in firstList will be compared with the 3 in secondList. These pairs are equal, so the method returns true even though the last element of firstList is not equal to the first element of secondList. To correct the error, the loop should iterate over the entire firstList rather than just over the first half—in other words, the terminating condition for the loop should be j < firstList.size() instead of j < firstList.size() / 2.
Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; } elements[possibleIndex] = temp; // line 12 } } The following declaration and method call appear in a method in the same class as insertionSort. int[] nums = {8, 7, 5, 4, 2, 1}; insertionSort(nums); How many times is the statement elements[possibleIndex] = temp; in line 12 of the method executed as a result of the call to insertionSort ? A 3 B 4 C 5 D 6 E 7
Answer C Correct. The statement in line 12 executes once each time an element is inserted into the array. The number of insertions is one less than the length of the array. For this array, the statement is executed for a total of five times: when 7 is inserted before 8, when 5 is inserted before 7, when 4 is inserted before 5, when 2 is inserted before 4, and when 1 is inserted before 2.
Consider the following correct 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[] arr = {30, 40, 10, 50, 20}; selectionSort(arr); 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
Answer C Correct. The statement in line 19 executes each time a value is swapped into the correct position in the array. For the given array, the values 30 and 10 are swapped. Then the values 40 and 20 are swapped. Then, since 30 is already in the correct position, no swap occurs. Lastly, 50 and 40 are swapped.
Consider the following correct implementation of the insertion sort algorithm. public static void insertionSort(int[] elements) { for (int j = 1; j < elements.length; j++) { int temp = elements[j]; int possibleIndex = j; while (possibleIndex > 0 && temp < elements[possibleIndex - 1]) { elements[possibleIndex] = elements[possibleIndex - 1]; possibleIndex--; // line 10 } elements[possibleIndex] = temp; } } The following declaration and method call appear in a method in the same class as insertionSort. int[] arr = {10, 8, 3, 4}; insertionSort(arr); How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call to insertionSort ? A 0 B 1 C 4 D 5 E 6
Answer D Correct. The while loop iterates once each time an array element is shifted to the right as a result of an insertion. Therefore, the statement in line 10 is executed each time an element is shifted to the right. For the given array, 10 is shifted right when 8 is inserted before it. Then 8 and 10 are each shifted right when 3 is inserted before them. Lastly, 8 and 10 are each shifted right when 4 is inserted before them. A total of five shifts occur, so the statement in line 10 is executed five times.
Consider the method seqSearch, which implements a sequential search algorithm. public int seqSearch(int[] arr, int target) { for (int j = 0; j < arr.length; j++) { if (arr[j] == target) { return j; } } return -1; } Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop. public int seqSearch2(int[] arr, int target) { for (int j : arr) { if (j == target) { return j; } } return -1; } Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the modification? A The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does. B The modification in seqSearch2 will cause a compilation error. C The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs. D The modification in seqSearch2 will cause -1 to be returned for all inputs. E The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases where target appears in arr.
Answer E Correct. The enhanced for loop stores the values of each array element in the variable j. Each array element is compared to target. If the array element is equal to target, j is returned, which is equal to the value of target.