加载中...
加载中...
Assessment for Unit 4: Data Collections
Select the one best answer for each question.
Questions 1-3 refer to the following scenario.
1. Which of the following describes the most significant ethical risk the company must consider regarding its data collection practices?
2. During testing, the team notices that the recommendation algorithm consistently promotes content from a specific demographic group while suppressing content from others. This is an example of:
3. The company wants to add a feature that suggests local businesses to users. Which of the following data sets would be most appropriate and ethically sound to use for this feature?
4. Consider the following algorithm described in plain language: Start with a variable count set to zero. Examine each number in a list of student test scores. For each score, if it is greater than or equal to 90, add one to count. After checking all scores, the value of count is the result. What is this algorithm determining?
5. Which of the following code segments correctly declares and initializes an array of 100 double values?
6. Consider the following code segment: ```java String[] words = new String[5]; ``` words[1] = "AP"; words[2] = "CS"; words[4] = "A"; ```java System.out.println(words[0]); System.out.println(words[3]); ``` What is printed when this code segment is executed?
7. Consider the following array of integers. ```java int[] nums = {10, 20, 30, 40, 50}; ``` Which of the following code segments will cause an ArrayIndexOutOfBoundsException?
8. Consider the following code segment: ```java int[] data = {3, 8, 2, 5, 10}; for (int x : data) { x = x * 2; } System.out.println(data[1]); ``` What is printed when this code segment is executed?
Questions 9-11 refer to the following method.
9. What does the processArray method do?
10. What is the value returned by the call processArray(new int[]{25, 15, 30, 10, 20})?
11. If the line if (arr[i] < result) were changed to if (arr[i] % 2 == 0), and assuming the method was changed to return a boolean, what would the modified method determine?
12. Consider the following incomplete method, which is intended to shift all elements of an array one position to the left and move the first element to the last position. For example, {1, 2, 3, 4} becomes {2, 3, 4, 1}. ```java public void shiftLeft(int[] nums) { int first = nums[0]; for (int i = 0; i < nums.length - 1; i++) { ``` /* missing code */ ```java } ``` nums[nums.length - 1] = first; ```java } ``` Which of the following should replace /* missing code */ for the method to work as intended?
13. A program needs to read a large list of student names and their corresponding GPAs from a text file named students.txt. Which code segment correctly sets up a Scanner to read from this file? (Assume java.io.File, java.io.FileNotFoundException, and java.util.Scanner are imported.)
14. Which of the following statements explains why wrapper classes like Integer and Double are necessary in Java?
15. Consider the following code segment: ```java ArrayList<Integer> scores = new ArrayList<Integer>(); ``` scores.add(95); ```java int firstScore = scores.get(0); ``` scores.add(new Integer(88)); Which line of code demonstrates autoboxing?
Questions 16-18 refer to the following code segment.
16. Which of the following code segments will insert "Cereal" between "Bread" and "Milk"?
17. Consider the following code segment, which is executed after the initial inventory list is created. inventory.set(0, "Oranges"); ```java String removed = inventory.remove(3); ``` inventory.add(1, "Juice"); What is the content of inventory after this code is executed?
18. What is the value of inventory.size() after the code in the previous question is executed?
19. A programmer wants to write a method to remove all strings of length 3 from an ArrayList<String>. Method I: ```java public void removeShort(ArrayList<String> words) { for (int i = 0; i < words.size(); i++) { if (words.get(i).length() == 3) { ``` words.remove(i); ```java } } } ``` Method II: ```java public void removeShort(ArrayList<String> words) { for (String word : words) { if (word.length() == 3) { ``` words.remove(word); ```java } } } ``` Which of the following statements is true?
20. Which of the following is the most appropriate way to traverse an ArrayList<Double> named prices to print each price, if the index of each element is NOT needed?
Questions 21-22 refer to the following method.
21. Consider the following ArrayList: list = {-5, 10, -15, 20, -25}. What is the state of list after the call mystery(list)?
22. What is the primary purpose of Line X (i--;) in the mystery method?
23. Which of the following correctly creates a 2D array to represent a grid with 5 rows and 10 columns of integers?
24. Consider the following 2D array initialization: ```java int[][] matrix = {{1, 2, 3}, {4, 5, 6}}; ``` What is the value of matrix[1][2]?
25. Given the same matrix from the previous question, what is the value of matrix.length?
26. Given the same matrix from the previous question, what is the value of matrix[0].length?
Questions 27-29 refer to the following code segment.
27. What is the value stored in grid[2][3] after the code executes?
28. The traversal implemented by the nested loops is best described as:
29. How would the loops be changed to traverse the grid in column-major order?
30. Consider the following method and 2D array. ```java public static int findSum(int[][] matrix) { int sum = 0; for (int c = 0; c < matrix[0].length; c++) { sum += matrix[1][c]; } return sum; } int[][] data = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}}; ``` What is returned by the call findSum(data)?
31. A method isSquare is intended to return true if a 2D array has the same number of rows and columns, and false otherwise. Which of the following implementations is correct?
Questions 32-33 refer to the following method.
32. Which of the following best describes the behavior of the linearSearch method?
33. Consider the array int[] arr = {5, 1, 8, 3, 1, 4};. What is returned by the call linearSearch(arr, 1)?
34. Under which condition will a binary search algorithm be significantly more efficient than a linear search algorithm?
35. What does the array look like after the first pass (one swap) of the selection sort algorithm?
36. What does the array look like after the second pass (two total swaps) of the selection sort algorithm?
37. Which of the following best describes the state of the array after k passes of selection sort?
38. What does the array look like after the element 8 has been processed by the insertion sort algorithm? (This is after the third element has been placed).
39. What does the array look like after the element 2 has been processed by the insertion sort algorithm?
40. Consider the following recursive method: ```java public int mystery(int n) { if (n == 0) { return 0; } else { return n + mystery(n - 1); } } ``` What is returned by the call mystery(4)?
41. Which of the following is the essential component of a recursive method that prevents infinite recursion?
42. Consider the following method: ```java public void printNums(int n) { if (n > 0) { ``` printNums(n - 1); ```java System.out.print(n + " "); } } ``` What is printed by the call printNums(3)?
Questions 43-45 refer to the following recursive method for binary search.
43. Consider the sorted array data = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}. A call is made: binarySearch(data, 72, 0, 9). What will be the values of low, mid, and high during the first recursive call?
44. Why is it essential that the array arr be sorted for this algorithm to work correctly?
45. Consider the sorted array data = {10, 20, 30, 40, 50}. A call is made: binarySearch(data, 25, 0, 4). What is the final return value?
46. Which standard algorithm is implemented by the following method? ```java public boolean hasProperty(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; } ```
47. Consider the following code segment: ```java ArrayList<String> list = new ArrayList<String>(); ``` list.add("a"); list.add("b"); list.add("c"); list.add(1, "x"); list.remove(2); list.set(0, "y"); ```java System.out.println(list); ``` What is printed?
48. A programmer wants to write a method sumColumn that calculates the sum of all elements in a specific column of a 2D integer array. ```java public int sumColumn(int[][] matrix, int colIndex) { int total = 0; ``` /* missing loop */ { ```java total += matrix[r][colIndex]; } return total; } ``` Which of the following should replace /* missing loop */?
49. Consider an array temps of double values. A programmer attempts to write a code segment to find the average temperature. ```java double sum = 0.0; for (int i = 1; i <= temps.length; i++) { // Line 1 sum += temps[i]; // Line 2 } double average = sum / temps.length; // Line 3 ``` Which line will cause an error?
50. Consider the following code. Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; ```java System.out.print((a == b) + " "); System.out.print(c == d); ``` Due to the way Java caches small Integer values, what is the most likely output?
51. Consider the following method. ```java public void reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; ``` arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; ```java } } ``` An array nums is initialized as {10, 20, 30, 40, 50}. What is the content of nums after reverse(nums) is called?
52. The following method is intended to return a new ArrayList containing all the elements from the input list that are longer than len. ```java public ArrayList<String> getLongWords(ArrayList<String> list, int len) { ArrayList<String> result = new ArrayList<String>(); for (String word : list) { if (word.length() > len) { ``` result.add(word); ```java } } return result; } ``` This algorithm pattern is best described as:
53. Consider the following 2D array and code segment. ```java String[][] data = {{"A", "B"}, {"C", "D"}, {"E", "F"}}; String result = ""; for (int c = 0; c < data[0].length; c++) { for (int r = 0; r < data.length; r++) { result += data[r][c]; } } System.out.println(result); ``` What is printed when the code segment is executed?
54. An array contains the values {1, 2, 3, 4, 5}. Which sorting algorithm would perform the minimum number of swaps to sort this array into ascending order?
55. A linear search is performed on an array of n elements. In the worst-case scenario, how many elements must be checked?
56. Consider the following recursive method. ```java public static int puzzle(int k) { if (k <= 1) { return 1; } return puzzle(k - 1) + puzzle(k - 2); } ``` What is the value returned by puzzle(4)?
57. A programmer needs to store a list of student ID numbers, which can grow as new students enroll. The ID numbers are integers. Which of the following is the most appropriate data structure to use?
58. What is the result of executing the following code segment? ```java int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; int[] arr3 = arr1; System.out.print(arr1 == arr2); System.out.print(" "); System.out.print(arr1 == arr3); ```
59. The method duplicate is intended to modify an ArrayList of Strings by adding a copy of each element immediately after the original element. For example, {"a", "b"} should become {"a", "a", "b", "b"}. ```java public void duplicate(ArrayList<String> list) { int originalSize = list.size(); for (int i = 0; i < originalSize; i++) { ``` /* missing code */ ```java } } ``` Which of the following could replace /* missing code */ for the method to work as intended?
60. Consider the following recursive method: ```java public void mysteryPrint(String str) { if (str.length() > 0) { System.out.print(str.substring(0, 1)); ``` mysteryPrint(str.substring(1)); ```java System.out.print(str.substring(0, 1)); } } ``` What is printed by the call mysteryPrint("AP")?