compsci test 7

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

Consider the following code segment. ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating); What is printed when this code segment is executed? A [9.84, 7.63, 6.24] B [9.84, 7.63, 7.65, 6.24] C [9.84, 8.93, 7.63] D [9.84, 8.93, 7.63, 6.24] E [9.84, 8.93, 7.65, 7.63]

C [9.84, 8.93, 7.63]

In the following code segment, assume that the ArrayList data has been initialized to contain the Integer values [4, 3, 4, 5, 3, 4]. int j = 0; while (j < data.size() - 1) { if (data.get(j) > data.get(j + 1)) { System.out.print(data.get(j + 1) + " "); } j++; } What, if anything, is printed as a result of executing the code segment? A 3 3 B 4 5 C 4 5 4 D Nothing is printed because the code segment does not compile. E Nothing is printed because an IndexOutOfBoundsException occurs.

A 3 3

In the code segment below, assume that the ArrayList object numbers has been properly declared and initialized to contain [0, 2, 4, 5]. for (int k = numbers.size() - 1; k >= 0; k--) { if (numbers.get(k) > k) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment? A 1 2 3 B 2 4 5 C 3 2 1 D 5 4 2 E Nothing will be printed because an IndexOutOfBoundsException will occur.

C 3 2 1

In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment? A 1 B 2 C 3 D 4 E 5

D 4

Consider the following statement, which is intended to create an ArrayList named years that can be used to store elements both of type Integer and of type String. /* missing code */ = new ArrayList(); Which of the following can be used to replace /* missing code */ so that the statement compiles without error? A ArrayList years B ArrayList years() C ArrayList years[] D ArrayList<Integer> years E ArrayList<String> years

A ArrayList years

Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. public static int countNegatives(ArrayList<Integer> arr) { int count = 0; for (int j = 0; j < arr.size(); j++) // Line 4 { if (arr.get(j) < 0) { count++; } } return count; } Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1 ? A. It has no impact on the behavior of the method. B. It causes the method to ignore the last element in arr. C. It causes the method to throw an IndexOutOfBounds exception. D. It reduces the size of arr by 1 and the last element will be removed. E. It changes the number of times the loop executes, but all indexes in arr will still be accessed.

A It has no impact on the behavior of the method.

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 = {9, 8, 7, 6, 5}; 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

B 2

Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. public static boolean inCommon(ArrayList<Integer> a, ArrayList<Integer> b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ? The change has no impact on the behavior of the method. After the change, the method will never check the first element in list b. After the change, the method will never check the last element in list b. After the change, the method will never check the first and the last elements in list b. The change will cause the method to throw an IndexOutOfBounds exception.

B After the change, the method will never check the first element in list b.

Consider the following code segment. ArrayList<String> animals = new ArrayList<>(); animals.add("fox"); animals.add(0, "squirrel"); animals.add("deer"); animals.set(2, "groundhog"); animals.add(1, "mouse"); System.out.println(animals.get(2) + " and " + animals.get(3)); What is printed as a result of executing the code segment? A mouse and fox B fox and groundhog C groundhog and deer D fox and deer E squirrel and groundhog

B fox and groundhog

Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of integers. For example, if nums contains {1, 2, 2, 3, 4, 3, 5, 5, 6}, then after executing remDups(nums), nums should contain {1, 2, 3, 4, 3, 5, 6}. public static void remDups(ArrayList<Integer> nums) { for (int j = 0; j < nums.size() - 1; j++) { if (nums.get(j).equals(nums.get(j + 1))) { nums.remove(j); j++; } } } The code does not always work as intended. Which of the following lists can be passed to remDups to show that the method does NOT work as intended? A {1, 1, 2, 3, 3, 4, 5} B {1, 2, 2, 3, 3, 4, 5} C {1, 2, 2, 3, 4, 4, 5} D {1, 2, 2, 3, 4, 5, 5} E {1, 2, 3, 3, 4, 5, 5}

B {1, 2, 2, 3, 3, 4, 5}

Consider the following method findValue, which takes an ArrayList of String elements and a String value as parameters and returns true if the String value is found in the list and false otherwise. public static boolean findValue(ArrayList<String> arr, String key) { for (int j = 0; j < arr.size(); j++) // Line 3 { if (arr.get(j).equals(key)) { return true; } } return false; } Which of the following best explains the impact to the findValue method when, in line 3, int j = 0 is replaced by int j = 1 ? Responses It has no impact on the behavior of the method. It will cause the method to return a different result when the key value is not in the list. It will cause the method to return a different result when the key value is found only at the first index in the list. It will cause the method to return a different result when the key value is found only at the last index in the list. It will cause the method to throw an array index out of bounds exception.

C It will cause the method to return a different result when the key value is found only at the first index in the list.

Consider the following code segment. ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment? A [100, 300, 400] B [200, 300] C [200, 400] D Nothing is printed because the code segment does not compile. E Nothing is printed because an IndexOutOfBoundsException will occur.

C [200, 400]

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 = {4, 12, 4, 7, 19, 6}; 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 2 B 3 C 4 D 5 E 6

D 5

Consider the following statement, which is intended to create an ArrayList named theater_club to store elements of type Student. Assume that the Student class has been properly defined and includes a no-parameter constructor. ArrayList<Student> theater_club = new /* missing code */; Which choice can replace /* missing code */ so that the statement compiles without error? A Student() B Student ArrayList() C ArrayList(Student) D ArrayList<Student>() E ArrayList<theater_club>()

D ArrayList<Student>()

Consider the following statement, which is intended to create an ArrayList named values that can be used to store Integer elements. /* missing code */ = new ArrayList<>(); Which of the following can be used to replace /* missing code */ so that the statement compiles without error? I. ArrayList values II. ArrayList<int> values III. ArrayList<Integer> values A I only B II only C III only D I and III only E II and III only

D I and III only

The removeElement method is intended to remove all instances of target from the ArrayList object data passed as a parameter. The method does not work as intended for all inputs. public void removeElement(ArrayList<Integer> data, int target) { for (int j = 0; j < data.size(); j++) { if (data.get(j).equals(target)) { data.remove(j); } } } Assume that the ArrayList object scores and the int variable low_score have been properly declared and initialized. In which of the following cases will the method call removeElement(scores, low_score) fail to produce the intended result? A When scores is [0, 2, 0, 2, 0, 6] and low_score is 0 B When scores is [2, 4, 0, 5, 7, 0] and low_score is 0 C When scores is [3, 4, 5, 7, 7, 2] and low_score is 1 D When scores is [8, 8, 4, 3, 3, 6] and low_score is 3 E When scores is [9, 9, 5, 9, 7, 7] and low_score is 5

D When scores is [8, 8, 4, 3, 3, 6] and low_score is 3

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; // Line 11 } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; } } } The following declaration and method call appear in the same class as selectionSort. int[] vals = {5, 10, 2, 1, 12}; selectionSort(vals); How many times is the statement minIndex = k; in line 11 of the method executed as a result of the call to selectionSort ? A 0 B 1 C 2 D 3 E 4

E 4

In the following code segment, assume that the ArrayList numList has been properly declared and initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the Integer value val in numList so that numList will remain in ascending order. The code segment does not work as intended in all cases. int index = 0; while (val > numList.get(index)) { index++; } numList.add(index, val); For which of the following values of val will the code segment not work as intended? A 0 B 1 C 2 D 3 E 4

E 4


Ensembles d'études connexes

Cosmetology State Board Prep (State Laws)

View Set

Chapter 36: Gastrointestinal Intubation and Special Nutritional Modalities

View Set