unit 7-8 part 1 (1-42, 77, 81)

¡Supera tus tareas y exámenes ahora con Quizwiz!

onsider the following data field and method. private int[][] mat; public void mystery () { for (int row = 1; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row != col) mat[row][col] = mat[row - 1][col]; } } } Assume that mat contains the following values. Note that mat[0][4] is 2. 4 1 3 4 2 1 8 7 5 3 7 4 6 9 2 3 8 1 2 4 5 6 7 0 3 What values does mat contain after a call to mystery? A 4 1 3 4 2 4 8 3 4 2 4 8 6 4 2 4 8 6 2 2 4 8 6 2 3 B 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 4 1 3 4 2 C 4 1 3 4 2 4 1 3 4 2 1 8 7 5 3 7 4 6 9 2 3 8 1 2 4 D 4 4 4 4 4 1 1 1 1 1 7 7 7 7 7 3 3 3 3 3 5 5 5 5 5 E 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3 4 8 6 2 3

A

1) Consider the following code segment. int[][] anArray = new int[10][8]; for (int j = 0; j < 8; j++) { for (int k = 0; k < 10; k++) { anArray[j][k] = 5; } } The code segment causes an ArrayIndexOutOfBoundsException to be thrown. How many elements in anArray will be set to 5 before the exception is thrown? A 0 B 8 C 9 D 64 E 80

B

Consider the following code segment. ArrayList<Integer> numList = new ArrayList<Integer>(); numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); numList.set(0, 2); System.out.print(numList); What is printed by the code segment? A [1, 3, 0, 1] B [2, 0, 2, 1] C [2, 0, 2, 3] D [2, 3, 2, 1] E [3, 0, 0, 1]

B

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

Consider the following two-dimensional array definition. int[][] data = new int[5][10]; Consider the following code segment, where all elements in data have been initialized. for (int j = 0; j < data.length; j++) { for (int k = 0; k < data[0].length; k++) { if (j == k) { System.out.println(data[j][k]); } } } How many times is the println method called when the code segment is executed? A 4 B 5 C 9 D 10 E 15

B

Consider the following code segment from an insertion sort program. for (int j = 1; j < arr.length; j++) { int insertItem = arr[j]; int k = j - 1; while(k >= 0 && insertItem < arr[k]) { arr[k+1] = arr[k]; k--; } arr[k + 1] = insertItem; /* end of for loop */ } Assume that array arr has been defined and initialized with the values {5, 4, 3, 2, 1}. What are the values in array arr after two passes of the for loop(i.e., when j = 2 at the point indicated by /* end of for loop */)? A {2, 3, 4, 5, 1} B {3, 2, 1, 4, 5} C {3, 4, 5, 2, 1} D {3, 5, 2, 3, 1} E {5, 3, 4, 2, 1}

C

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

Consider the following code segment. List<String> students = new ArrayList<String>(); students.add("Alex"); students.add("Bob"); students.add("Carl"); for (int k = 0; k < students.size(); k++) { System.out.print(students.set(k, "Alex") + " "); } System.out.println(); for (String str : students) { System.out.print(str + " "); } What is printed as a result of executing the code segment? Select one: a. Alex Alex Alex Alex Alex Alex b. Alex Alex Alex Alex Bob Carl c. Alex Bob Carl Alex Alex Alex d. Alex Bob Carl Alex Bob Carl e. Nothing is printed because the first print statement will cause a runtime exception to be thrown.

C

Consider the following method. public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = 0; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; } 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]

C

3) Assume that mat has been declared as a 4×4 array of integers and has been initialized to contain all 1s. Consider the following code segment. int n = mat.length; for (int j = 1; j < n; j++) { for (int k = 1; k < n; k++) { mat[j][k] = mat[j - 1][k] + mat[j][k - 1]; } } What is the value of mat[2][2] after the code segment has completed execution? A 2 B 3 C 4 D 6 E 10

D

Consider the following code segment, which is intended to declare and initialize the two-dimensional (2D) String array things. /* missing code */ = {{"spices", "garlic", "onion", "pepper"}, {"clothing", "hat", "scarf", "gloves"}, {"plants", "tree", "bush", "flower"}, {"vehicles", "car", "boat", "airplane"}}; Which of the following could replace /* missing code */ so that things is properly declared? A new String[][] things B new(String[][]) things C String[] String[] things D String[][] things E [][]String things

D

Consider the following code segment. ArrayList<String> colors = new ArrayList<String>(); colors.add("Red"); colors.add("Orange"); colors.set(1, "Yellow"); colors.add(1, "Green"); colors.set(colors.size() - 1, "Blue"); colors.remove(0); System.out.println(colors); What is printed as a result of executing the code segment? A [Red, Orange] B [Red, Green] C [Yellow, Blue] D [Green, Blue] E [Blue, Yellow]

D

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

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]

D

Consider the following instance variable and method. Method wordsWithCommas is intended to return a string containing all the words in listOfWords contains ["one", "two", "three"], the string returned by the call wordsWithCommas() should be "{one, two, three}". private List listOfWords; public String wordsWithCommas() { String result = "{"; int sizeOfList = /* expression */; for (int k=0; k < sizeOfList; k++) { result = result + listOfWords.get(k); if ( /* condition */ ) { result = result + ", "; } } result = result + "}"; return result; } Which of the following can be used to replace /* expression */ and /* condition */ so that wordsWithCommas 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

D

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

D

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

Which of the following is a reason to use an ArrayList instead of an array? A An ArrayList allows faster access to its kth item than an array does. B An ArrayList always uses less memory than an array does. C An ArrayList can store objects and an array can only store primitive types. D An ArrayList resizes itself as necessary when items are added, but an array does not. E An ArrayList provides access to the number of items it stores, but an array does not.

D

Consider the following code segment. int[][] array2D = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; for (int[] i : array2D) { for (int x : i) { System.out.print(x + " "); } System.out.println(" "); } How many times will the statement System.out.print(x + " ") be executed? A 3 times B 4 times C 6 times D 12 times E 16 times

E

A two-dimensional array arr is to be created with the following contents. boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ? A boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true; B boolean arr[][] = new boolean[2][3]; arr[1][2] = true; arr[2][3] = true; C boolean arr[][] = new boolean[3][2]; arr[0][1] = true; arr[1][2] = true; D boolean arr[][] = new boolean[3][2]; arr[1][0] = true; arr[2][1] = true; E boolean arr[][] = new boolean[3][2]; arr[2][1] = true; arr[3][2] = true;

A

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums); What is printed as a result of executing the code segment? A [1, 2, 0] B [1, 3, 0] C [1, 3, 2] D [1, 37, 3, 0] E [37, 0, 0]

A

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

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

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

Consider the following correct implementation of the insertion sort algorithm. The insertionSort method correctly sorts the elements of ArrayList data into increasing order. public static void insertionSort(ArrayList<Integer> data) { for (int j = 1; j < data.size(); j++) { int v = data.get(j); int k = j; while (k > 0 && v < data.get(k - 1)) { data.set(k, data.get(k - 1)); /* Statement 1 */ k--; } data.set(k, v); /* Statement 2 */ /* End of outer loop */ } } Assume that insertionSort is called with an ArrayList parameter that has been initialized with the following Integer objects. [1, 2, 3, 4, 5, 6] How many times will the statements indicated by /* Statement 1 */ and /* Statement 2 */ execute? A Statement 1: 0 Statement 2: 0 B Statement 1: 0 Statement 2: 5 C Statement 1: 0 Statement 2: 6 D Statement 1: 5 Statement 2: 5 E Statement 1: 6 Statement 2: 6

B

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

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--) ? A The change has no impact on the behavior of the method. B After the change, the method will never check the first element in list b. C After the change, the method will never check the last element in list b. D After the change, the method will never check the first and the last elements in list b. E The change will cause the method to throw an IndexOutOfBounds exception.

B

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

Consider the following code segment. ArrayList<String> items = new ArrayList<String>(); items.add("A"); items.add("B"); items.add("C"); items.add(0, "D"); items.remove(3); items.add(0, "E"); System.out.println(items); What is printed as a result of executing the code segment? A [A, B, C, E] B [A, B, D, E] C [E, D, A, B] D [E, D, A, C] E [E, D, C, B]

C

Consider the following correct implementation of the insertion sort algorithm. The insertionSort method correctly sorts the elements of ArrayList data into increasing order. public static void insertionSort(ArrayList<Integer> data) { for (int j = 1; j < data.size(); j++) { int v = data.get(j); int k = j; while (k > 0 && v < data.get(k - 1)) { data.set(k, data.get(k - 1)); /* Statement 1 */ k--; } data.set(k, v); /* Statement 2 */ /* End of outer loop */ } } Assume that insertionSort has been called with an ArrayList parameter that has been initialized with the following Integer objects. [5, 2, 4, 1, 3, 6] What will the contents of data be after three passes of the outside loop (i.e., when j == 3 at the point indicated by /* End of outer loop */) ? A [1, 2, 3, 4, 5, 6] B [1, 2, 3, 5, 4, 6] C [1, 2, 4, 5, 3, 6] D [2, 4, 5, 1, 3, 6] E [5, 2, 1, 3, 4, 6]

C

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 ? A It has no impact on the behavior of the method. B It will cause the method to return a different result when the key value is not in the list. 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. D It will cause the method to return a different result when the key value is found only at the last index in the list. E It will cause the method to throw an array index out of bounds exception.

C

Consider the following method, which is intended to return the number of columns in the two-dimensional array arr for which the sum of the elements in the column is greater than the parameter val. public int countCols(int[][] arr, int val) { int count = 0; for (int col = 0; col < arr[0].length; col++) // Line 5 { int sum = 0; for (int[] row : col) // Line 8 { sum += row[col]; // Line 10 } if (sum > val) { count++; } } return count; } The countCols method does not work as intended. Which of the following changes should be made so the method works as intended? A Line 5 should be changed to for (int col = 0; col < arr.length; col++) B Line 8 should be changed to for (int row : col) C Line 8 should be changed to for (int[] row : arr) D Line 10 should be changed to sum += arr[col]; E Line 10 should be changed to sum += arr[row][col];

C

Consider the following code segment, where num is a properly declared and initialized integer variable. The code segment is intended to traverse a two-dimensional (2D) array arr looking for a value equal to num and then print the value. The code segment does not work as intended. int[][] arr = {{7, 3, 6, 4}, {9, 2, 0, 5}, {1, 4, 3, 8}}; for (int j = 0; j < arr.length - 1; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.println(arr[j][k]); } } } For which of the following values of num does the code segment not work as intended? A num = 5 B num = 6 C num = 7 D num = 8 E num = 9

D

Consider the following code segment. int[][] arr = {{6, 2, 5, 7}, {7, 6, 1, 2}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] > j + k) { System.out.println("!"); } } } How many times will "!" be printed when the code segment is executed? A 0 times B 2 times C 4 times D 6 times E 8 times

D

Consider the following code segment. int[][] mat = new int[3][4]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) { mat[row][col] = 1; } else if (row == col) { mat[row][col] = 2; } else { mat[row][col] = 3; } } } What are the contents of mat after the code segment has been executed? a. {{2, 1, 1}, {3, 2, 1}, {3, 3, 2}, {3, 3, 3}} b. {{2, 3, 3}, {1, 2, 3}, {1, 1, 2}, {1, 1, 1}} c. {{2, 3, 3, 3}, {1, 2, 3, 3}, {1, 1, 2, 3}} d. {{2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1}} e. {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}}

D

Consider the following method, count, which is intended to traverse all the elements in the two-dimensional (2D) String array things and return the total number of elements that contain at least one "a". public static int count(String[][] things) { int count = 0; for (int r = 0; r < things.length; r++) { for (int c = 0; c < things[r].length - 1; c++) { if (things[r][c].indexOf("a") >= 0) { count++; } } } return count; } For example, if things contains {{"salad", "soup"}, {"water", "coffee"}}, then count(things) should return 2. The method does not always work as intended. For which of the following two-dimensional array input values does count NOT work as intended? A {{"lemon"}, {"lime"}} B {{"tall", "short"}, {"up", "down"}} C {{"rabbit", "bird"}, {"cat", "dog"}, {"gecko", "turtle"}} D {{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}} E {{"math", "english", "physics"}, {"golf", "baseball", "soccer"}}

D

Consider the following method, which is intended to return true if 0 is found in its two-dimensional array parameter arr and false otherwise. The method does not work as intended. public boolean findZero(int[][] arr) { for (int row = 0; row <= arr.length; row++) { for (int col = 0; col < arr[0].length; col++) { if (arr[row][col] == 0) { return true; } } } return false; } Which of the following values of arr could be used to show that the method does not work as intended? A {{30, 20}, {10, 0}} B {{4, 3}, {2, 1}, {0, -1}} C {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}} D {{5, 10, 15, 20}, {25, 30, 35, 40}} E {{10, 20, 0, 30, 40}, {60, 0, 70, 80, 90}}

D

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

The following sort method correctly sorts the integers in elements into ascending order. Line 1: public static void sort(int[] elements) Line 2: { Line 3: for (int j = 0; j < elements.length - 1; j++) Line 4: { Line 5: int index = j; Line 6: Line 7: for (int k = j + 1; k < elements.length; k++) Line 8: { Line 9: if (elements[k] < elements[index]) Line 10: { Line 11: index = k; Line 12: } Line 13: } Line 14: Line 15: int temp = elements[j]; Line 16: elements[j] = elements[index]; Line 17: elements[index] = temp; Line 18: } Line 19: } Which of the following changes to the sort method would correctly sort the integers in elements into descending order? I. Replace line 9 with: Line 9: if (elements[k] > elements[index]) II. Replace lines 15-17 with: Line 15: int temp = elements[index]; Line 16: elements[index] = elements[j]; Line 17: elements[j] = temp; III. Replace line 3 with: Line 3: for (int j = elements.length − 1; j > 0; j−−) and replace line 7 with: Line 7: for (int k = 0; k < j; k++) a. I only b. II only c. I and II only d. I and III only e. I, II, and III

D

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

/** Precondition: values has at least one row */ public static int calculate (int[][] values) { int found = values[0][0]; int result = 0; for(int [] row: values) { for(int y = 0; y < row.length; y++) { if(row[y] > found) { found = row[y]; result = y; } } } return result; } Which of the following best describes what is returned by the calculate method? A The largest value in the two-dimensional array B The smallest value in the two-dimensional array C The row index of an element with the largest value in the two-dimensional array D The row index of an element with the smallest value in the two-dimensional array E The column index of an element with the largest value in the two-dimensional array

E

2) Assume mat is defined as follows. int dim = 4; int[][] mat = new int[dim][dim]; Consider the following code segment. int sum = 0; for (int row = 0; row < dim; row++) { sum = sum + mat[row][dim - 1]; } Assume that mat contains the following values before the code segment is executed. Note that mat[0][3] is 2. 0 1 2 3 0 1 1 2 2 1 1 2 2 4 2 1 3 2 6 3 1 4 2 8 What value will sum contain after the code segment is executed? A 6 B 8 C 13 D 15 E 20

E

Consider the following code segment. String[][] letters = {{"A", "B", "C", "D"}, {"E", "F", "G", "H"}, {"I", "J", "K", "L"}}; for (int col = 1; col < letters[0].length; col++) { for (int row = 1; row < letters.length; row++) { System.out.print(letters[row][col] + " "); } System.out.println(); } What is printed as a result of executing this code segment? A) A E I F J K B) B F J C G K D H L C) E I F J G K H L D) F G H J K L E) F J G K H L

E

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


Conjuntos de estudio relacionados

Microbiology-Chapter 12 Homework

View Set

*Relias - Corporate Compliance: The Basics

View Set

Chapter 9- Pain Mangement Prep U Questions

View Set

Doing Business Internationally and Regulations

View Set

MGMT 2100- Chapter 4: Ethics and Social Responsibility

View Set

type or die roblox longest answers

View Set