apcsa final unit 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following method. public int[] addNum(int[] array, int first, int second, int num) { int[] newArray = new int[array.length]; newArray[first] = array[first] + num; newArray[second] = array[second] + num; return newArray; } Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0} ? A int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); B int[] array1 = {-5, -5, 13, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5); C int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 3, 5, 5); D int[] array1 = {5, 8, 2, 4, 6, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5); E int[] array1 = {0, -5, 8, 0, 9, 0, 0}; int[] array2 = addNum(array1, 2, 4, 5);

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. (a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true? A Only when a is true B Only when b is true C Only when both a and b are true D The expression will never evaluate to true. E The expression will always evaluate to true.

Only when b is true

Consider an integer array nums, which has been properly declared and initialized with one or more values. Which of the following code segments counts the number of negative values found in nums and stores the count in counter ? int counter = 0;int i = -1;while (i <= nums.length - 2){ i++; if (nums[i] < 0) { counter++; }} int counter = 0;for (int i = 1; i < nums.length; i++){ if (nums[i] < 0) { counter++; }} int counter = 0;for (int i : nums){ if (nums[i] < 0) { counter++; }} A I only B II only C I and II only D I and III only E I, II, and III

I only

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ? A arr[0] = 5; arr[5] = 0; B arr[0] = arr[5]; arr[5] = arr[0]; C int k = arr[5]; arr[0] = arr[5]; arr[5] = k; D int k = arr[0]; arr[0] = arr[5]; arr[5] = k; E int k = arr[5]; arr[5] = arr[0]; arr[0] = arr[5];

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

Which of the following code segments produces the output "987654321" ? A int num = 10; while (num > 0) { System.out.print(num); num--; } B int num = 10; while (num >= 0) { System.out.print(num); num--; } C int num = 10; while (num > 1) { num--; System.out.print(num); } D int num = 10; while (num >= 1) { num--; System.out.print(num); } E int num = 0; while (num <= 9) { System.out.print(10 - num); num++; }

int num = 10; while (num > 1) { num--; System.out.print(num); }

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int i = 1; i < arr.length; i += 2) { arr[i] = arr[i - 1]; } Which of the following represents the contents of the array arr after the code segment is executed? A {0, 1, 2, 3, 4, 5, 6} B {1, 1, 1, 1, 1, 1, 1} C {1, 1, 3, 3, 5, 5, 7} D {1, 2, 3, 4, 5, 6, 7} E {2, 2, 4, 4, 6, 6, 7}

{1, 1, 3, 3, 5, 5, 7}

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.

3 2 1

Consider the following code segment. int[] arr = {3, 1, 0, 4, 2}; for(int j = 0; j < arr.length; j++) { System.out.print(arr[j] + j + " "); } What, if anything, is printed as a result of executing the code segment? A 3 1 0 4 2 B 3 2 2 7 6 C 6 2 0 8 4 D 7 2 3 6 2 E Nothing is printed, because an ArrayIndexOutOfBoundsException is thrown.

3 2 2 7 6

Consider the following methods, which appear in the same class. public void slope(int x1, int y1, int x2, int y2) { int xChange = x2 - x1; int yChange = y2 - y1; printFraction(yChange, xChange); } public void printFraction(int numerator, int denominator) { System.out.print(numerator + "/" + denominator); } Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as a result of the method call? A 8/4 B 5/1 C 4/8 D 2/1 E 1/5

8/4

Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ? A a && b && c B a || b || c C !a && !b || c D !a && !b && c E !a || !b || c

!a || !b || c

Consider the following code segment. int[] arr = {4, 2, 9, 7, 3}; for (int k : arr) { k = k + 10; System.out.print(k + " "); } for (int k : arr) System.out.print(k + " "); What is printed as a result of executing the code segment? A 0 1 2 3 4 0 1 2 3 4 B 4 2 9 7 3 4 2 9 7 3 C 10 11 12 13 14 0 1 2 3 4 D 14 12 19 17 13 4 2 9 7 3 E 14 12 19 17 13 14 12 19 17 13

14 12 19 17 13 4 2 9 7 3

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

ArrayList years

The method countTarget below is intended to return the number of times the value target appears in the array arr. The method may not work as intended. public int countTarget(int[] arr, int target) { int count = 0; for (int j = 0; j <= arr.length; j++) // line 4 { if (arr[j] == target) { count++; } } return count; } Which of the following changes, if any, can be made to line 4 so that the method will work as intended? A Changing int j = 0; to int j = 1; B Changing j <= arr.length; to j < arr.length; C Changing j <= arr.length; to j < arr.length - 1; D Changing j <= arr.length; to j < arr.length + 1; E No change is necessary; the method works correctly as is.

Changing j <= arr.length; to j < arr.length;

Consider the following Boolean expressions. I. A && B II. !A && !B Which of the following best describes the relationship between values produced by expression I and expression II? A Expression I and expression II evaluate to different values for all values of A and B. B Expression I and expression II evaluate to the same value for all values of A and B. C Expression I and expression II evaluate to the same value only when A and B are the same. D Expression I and expression II evaluate to the same value only when A and B differ. E Expression I and expression II evaluate to the same value whenever A is true.

Expression I and expression II evaluate to the same value only when A and B differ.

Consider the following class definition. public class Value { private int num; public int getNum() { return num; } // There may be instance variables, constructors, and methods not shown. } The following method appears in a class other than Value. It is intended to sum all the num instance variables of the Value objects in its ArrayList parameter. /** Precondition: valueList is not null */ public static int getTotal(ArrayList<Value> valueList) { int total = 0; /* missing code */ return total; } Which of the following code segments can replace /* missing code */ so the getTotal method works as intended? for (int x = 0; x < valueList.size(); x++){ total += valueList.get(x).getNum();} for (Value v : valueList){ total += v.getNum();} for (Value v : valueList){ total += getNum(v);} A I only B II only C III only D I and II E I and III

I and II

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? ArrayList values ArrayList<int> values ArrayList<Integer> values A I only B II only C III only D I and III only E II and III only

I and III only

Consider the following class. public class SomeMethods {public void one(int first) { / * implementation not shown * / } public void one(int first, int second) { / * implementation not shown * / } public void one(int first, String second) { / * implementation not shown * / } } Which of the following methods can be added to the SomeMethods class without causing a compile-time error? public void one(int value){ / * implementation not shown * / } public void one (String first, int second) { / * implementation not shown * / } public void one (int first, int second, int third) { / * implementation not shown * / } A I only B I and II only C I and III only D II and III only E I, II, and III

II and III only

Consider an integer array, nums, which has been declared and initialized with one or more integer values. Which of the following code segments updates nums so that each element contains the square of its original value? I. int k = 0; while (k < nums.length) { nums[k] = nums[k] * nums[k]; } II. for (int k = 0; k < nums.length; k++) { nums[k] = nums[k] * nums[k]; } III. for (int n : nums) { n = n * n; } A II only B I and II only C I and III only D II and III only E I, II, and III

II only

Consider the following method. /** Removes all occurrences of nameToRemove from nameList. * @param nameList a list of names * @param nameToRemove a name to be removed from nameList */ public void removeName(List<String> nameList, String nameToRemove) { /* missing implementation */ } Which of the following can be used to replace /* missing implementation */ so that removeName will work as intended? for (String name : nameList) { if (name.equals(nameToRemove)) name.remove(); } for (int k = 0; k < nameList.size(); k++) { if (nameList.get(k).equals(nameToRemove)) nameList.remove(k); } for (int k = nameList.size() - 1; k >= 0; k--) { if (nameList.get(k).equals(nameToRemove)) nameList.remove(k); } A I only B II only C III only D II and III only E I, II, and III

III only

Consider the following method, which is intended to calculate and return the expression (x+y)2|a−b|−−−−−√(x+y)2|a−b|. public double calculate(double x, double y, double a, double b) { return /* missing code */; } Which of the following can replace /* missing code */ so that the method works as intended? A Math.sqrt(x ^ 2, y ^ 2, a - b) B Math.sqrt((x + y) ^ 2) / Math.abs(a, b) C Math.sqrt((x + y) ^ 2 / Math.abs(a - b)) D Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b)) E Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

In the following code segment, assume that the string str has been properly declared and initialized. The code segment is intended to print the number of strings in the array animals that have str as a substring. String[] animals = {"horse", "cow", "goat", "dog", "cat", "mouse"}; int count = 0; for (int i = 0; i <= animals.length; i++) { if (animals[i].indexOf(str) >= 0) { count++; } } System.out.println(count); The code segment does not work as intended. Which of the following changes should be made so the code segment works as intended? A The Boolean expression in the for loop header should be changed to i < animals.length. B The Boolean expression in the for loop header should be changed to i < animals.length - 1. C The Boolean expression in the for loop header should be changed to i < animals[i].length. D The condition in the if statement should be changed to animals[i].equals(str). E The condition in the if statement should be changed to animals[i].substring(str).

The Boolean expression in the for loop header should be changed to i < animals.length.

Consider the following method, which is intended to return a list containing the elements of the parameter myList with all even elements removed. public static ArrayList<Integer> removeEvens(ArrayList<Integer> myList) { for (int i = 0; i < myList.size(); i++) { if (myList.get(i) % 2 == 0) { myList.remove(i); } } return myList; } Which of the following best explains why the code segment does not work as intended? A The code segment causes an IndexOutOfBoundsException for all lists because of an incorrect Boolean expression in the for loop. B The code segment causes an IndexOutOfBoundsException for lists with at least one even element because the indexes of all subsequent elements change by one when a list element is removed. C The code segment returns a list with fewer elements than intended because it fails to consider the last element of myList. D The code segment removes the wrong elements of myList because the condition in the if statement to test whether an element is even is incorrect. E The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.

The code segment skips some elements of myList because the indexes of all subsequent elements change by one when a list element is removed.

Consider the following code segment. int j = 1; while (j < 5) { int k = 1; while (k < 5) { System.out.println(k); k++; } j++; } Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ? A There will be one more value printed because the outer loop will iterate one additional time. B There will be four more values printed because the outer loop will iterate one additional time. C There will be one less value printed because the outer loop will iterate one fewer time. D There will be four fewer values printed because the outer loop will iterate one fewer time. E There will be no change to the output of the code segment.

There will be four more values printed because the outer loop will iterate one additional time.

Assume obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true? A When obj1 and obj2 are defined within the same method B When obj1 and obj2 are instances of the same class C When obj1 and obj2 refer to objects that contain the same data D When obj1 and obj2 refer to the same object E When obj1 and obj2 are private class variables defined in the same class

When obj1 and obj2 refer to the same object

Consider the following method. Assume that a List<Integer> values initially contains the following Integer values. What will values contain as a result of executing mystery(values) ? A [0, 0, 4, 2, 5, 0, 3, 0] B [4, 2, 5, 3] C [0, 0, 0, 0, 4, 2, 5, 3] D [0, 4, 2, 5, 3] E The code throws an ArrayIndexOutOfBoundsException exception.

[0, 4, 2, 5, 3]

Consider the following method. Which of the following is printed as a result of executing the following statement?System.out.println(mystery ( 6 ) ) ; A [3, 4, 7, 12, 19, 28] B [3, 4, 7, 12, 19, 28, 39] C [4, 7, 12, 19, 28, 39] D [39, 28, 19, 12, 7, 4] E [39, 28, 19, 12, 7, 4, 3]

[4, 7, 12, 19, 28, 39]

Consider the following data field and method. private ArrayList list; public void mystery(int n) { for (int k = 0; k < n; k++) { Object obj = list.remove(0); list.add(obj); } } Assume that list has been initialized with the following Integer objects. [12, 9, 7, 8, 4, 3, 6, 11, 1] Which of the following represents the list as a result of a call to mystery(3)? A [12, 9, 8, 4, 3, 6, 11, 1, 7] B [12, 9, 7, 8, 4, 6, 11, 1, 3] C [12, 9, 7, 4, 3, 6, 11, 1, 8] D [8, 4, 3, 6, 11, 1, 12, 9, 7] E [1, 11, 6, 12, 9, 7, 8, 4, 3]

[8, 4, 3, 6, 11, 1, 12, 9, 7]

Consider the following two code segments where the int variable choice has been properly declared and initialized. Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Assume that both code segments initialize choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output? A choice < 5 B choice >= 5 and choice <= 10 C choice > 10 D choice == 5 or choice == 10 E There is no value for choice that will cause the two code segments to produce different output.

choice > 10

Consider the code segment below, where arr is a one-dimensional array of integers. int sum = 0; for (int n : arr) { sum = sum + 2 * n; } System.out.print(sum); Which of the following code segments will produce the same output as the code segment above? A int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); B int sum = 0; for (int k = 0; k <= arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); C int sum = 0; for (int k = 1; k <= arr.length; k++) { sum = sum + 2 * k; } System.out.print(sum); D int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum); E int sum = arr[0]; for (int k = 1; k <= arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum);

int sum = 0; for (int k = 0; k < arr.length; k++) { sum = sum + 2 * arr[k]; } System.out.print(sum);

Consider the following code segment, which is intended to simulate a random process. The code is intended to set the value of the variable event to exactly one of the values 1, 2, or 3, depending on the probability of an event occurring. The value of event should be set to 1 if the probability is 70 percent or less. The value of event should be set to 2 if the probability is greater than 70 percent but no more than 80 percent. The value of event should be set to 3 if the probability is greater than 80 percent. The variable randomNumber is used to simulate the probability of the event occurring. int event = 0; if (randomNumber <= 0.70) { event = 1; } if (randomNumber <= 0.80) { event = 2; } else { event = 3; } The code does not work as intended. Assume that the variable randomNumber has been properly declared and initialized. Which of the following initializations for randomNumber will demonstrate that the code segment will not work as intended? A randomNumber = 0.70; B randomNumber = 0.80; C randomNumber = 0.85; D randomNumber = 0.90; E randomNumber = 1.00;

randomNumber = 0.70;

Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal. Which of the following should replace / * missing code * / so that almostEqual will work as intended? A return (d1 - d2) <= tolerance; B return ((d1 + d2) / 2) <= tolerance; C return (d1 - d2) >= tolerance; D return ( (d1 + d2) / 2) >= tolerance; E return Math.abs(d1 - d2) <= tolerance;

return Math.abs(d1 - d2) <= tolerance;

Consider the following method that is intended to return the sum of the elements in the array key. Which of the following statements should be used to replace / * missing code * / so that sumArraywill work as intended? A sum = key [ i ] ; B sum += key [i - 1] ; C sum += key [ i ] ; D sum += sum + key[i - 1] ; E sum += sum + key [ i ] ;

sum += key [i - 1] ;

Consider the following interface and class declarations. Which of the following can be used to replace /* expression */ so that getTotalMileage returns the total of the miles traveled for all vehicles in the fleet? A getMileage (v) B myVehicles [v] .getMileage () C Vehicle.get (v) .getMileage () D myVehicles.get (v) .getMileage () E v.getMileage ()

v.getMileage ()

Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value? A valueOne.equals((Object) valueTwo) B valueOne == valueTwo C valueOne.compareTo((Object) valueTwo) == 0 D valueOne.compareTo(valueTwo) == 0 E valueOne.equals(valueTwo)

valueOne == valueTwo

Consider the following code segment. boolean[] oldVals = {true, false, true, true}; boolean[] newVals = new boolean[4]; for (int j = oldVals.length - 1; j >= 0; j--) { newVals[j] = !(oldVals[j]); } What, if anything, will be the contents of newVals as a result of executing the code segment? A {true, true, false, true} B {true, false, true, true} C {false, true, false, false} D {false, false, true, false} E The array newVals will not contain any values because the code segment does not compile.

{false, true, false, false}

A teacher put three bonus questions on a test and awarded 5 extra points to anyone who answered all three bonus questions correctly and no extra points otherwise. Assume that the boolean variables bonusOne, bonusTwo, and bonusThree indicate whether a student has answered the particular question correctly.Each variable was assigned true if the answer was correct and false if the answer was incorrect. Which of the following code segments will properly update the variable grade based on a student's performance on the bonus questions? I. if (bonusOne && bonusTwo && bonusThree) grade += 5; II. if (bonusOne || bonusTwo || bonusThree) grade += 5; III. if (bonusOne) grade += 5; if (bonusTwo) grade += 5; if (bonusThree) grade += 5; A I only B II only C III only D I and III E II and III

I only

Consider the following method that is intended to modify its parameter nameList by replacing all occurrences of name with newValue. public void replace(ArrayList<String> nameList, String name, String newValue) { for (int j = 0; j < nameList.size(); j++) { if ( /* expression */ ) { nameList.set(j, newValue); } } } Which of the following can be used to replace /* expression */ so that replace will work as intended? A nameList.get(j).equals(name) B nameList.get(j) == name C nameList.remove(j) D nameList[j] == name E nameList[j].equals(name)

nameList.get(j).equals(name)

Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k = 1; k <= 30; k = k + 2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2 ? A Code segment 1 and code segment 2 will produce the same output. B Code segment 2 will print the sum of only the even integers from 1 through 30, inclusive because it starts sum at zero, increments k by twos, and terminates when k exceeds 30. C Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30. D Code segment 2 will print the sum of only the even integers from 1 through 60, inclusive because it starts sum at zero, increments k by twos, and iterates 30 times. E Code segment 2 will print the sum of only the odd integers from 1 through 60, inclusive because it starts k at one, increments k by twos, and iterates 30 times.

Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.

Consider the following method, isSorted, which is intended to return true if an array of integers is sorted in nondecreasing order and to return false otherwise. Which of the following can be used to replace /* missing code */ so that isSorted will work as intended? A I only B II only C III only D I and II only E I and III only

I only

Consider the following instance variable and method. Method wordsWithCommas is intended to return a string containing all the words in listOfWords separated by commas and enclosed in braces.For example, if listOfWords contains ["one", "two", "three"], the string returned by the call wordsWithCommas () should be "{one, two, three}". Which of the following can be used to replace /* expression */ and /* condition */ so thatwordsWithCommas will work as intended? A / * expression * / / / * condition * / listOfWords.size() - 1 / k != 0 B / * expression * / / / * condition * /listOfWords.size() / k != 0 C / * expression * / / / * condition * / listOfWords.size() - 1 / k != sizeOfList - 1 D / * expression * / / / * condition * / listOfWords.size() / k != sizeOfList - 1 E / * expression * / / / * condition * /result.length() / k != 0

/ * expression * / / / * condition * / listOfWords.size() / k != sizeOfList - 1

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.

3 3

Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1 is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace the element of minArray with the corresponding element of arr1. After the code segment executes, minArray should hold the smaller of the two elements originally found at the same indices in arr1 and minArray and arr1 should remain unchanged. for (int c = 0; c < arr1.length; c++) { if (arr1[c] < minArray[c]) { arr1[c] = minArray[c]; } else { minArray[c] = arr1[c]; } } Which of the following changes will ensure that the code segment always works as intended? A Changing the Boolean expression in line 1 to c <= arr1.length B Changing the relational operator in line 3 to > C Removing lines 5-8 D Swapping the positions of line 5 and line 9 E Removing lines 7-10

Removing lines 5-8

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for (int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended? A The for loop header should be replaced with for (int k = 0; k < arr.length; k++). B The for loop header should be replaced with for (int k = 0; k <= arr.length; k--). C The for loop header should be replaced with for (int k = 1; k <= arr.length - 1; k++). D The statement in the body of the for loop should be replaced with sum += arr[0]. E The statement in the body of the for loop should be replaced with sum += arr[k - 1].

The for loop header should be replaced with for (int k = 0; k < arr.length; k++)

The following method is intended to remove all elements of an ArrayList of integers that are divisible by key and add the removed elements to a new ArrayList, which the method returns. public static ArrayList<Integer> match(ArrayList<Integer> numList, int key) { ArrayList<Integer> returnList = new ArrayList<Integer>(); int i = 0; while (i < numList.size()) { int num = numList.get(i); if (num % key == 0) { numList.remove(i); returnList.add(num); } i++; } return returnList; } As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16] and the parameter key has the value 5, then numList should contain [2, 16] at the end of the method and an ArrayList containing [5, 10, 20] should be returned. Which of the following best explains why the method does not always work as intended? A The method attempts to add an element to returnList after that element has already been removed from numList. B The method causes a NullPointerException to be thrown when no matches are found. C The method causes an IndexOutOfBoundsException to be thrown. D The method fails to correctly determine whether an element of numList is divisible by key. E The method skips some elements of numList during the traversal.

The method skips some elements of numList during the traversal.


Kaugnay na mga set ng pag-aaral

Lecture 7: rational choice theory

View Set

Initiation Exam - Baylor University

View Set

ASVAB: Arithmetic Reasoning Questions, *THIS ONE* ASVAB 2020 Arithmetic Reasoning

View Set

introduction to film and television aesthetics

View Set