Trevor Wylie

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

Consider the following code segment. public static int findMaxValue(int[][] arr) { int max = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } } return max; } Which of the following options identifies the error in the given code segment? A. The initial value of max should be set to Integer.MIN_VALUE instead of 0. B. The loop condition for the outer loop should be i < arr.length - 1. C. The loop condition for the inner loop should be j < arr.length - 1. D. The condition arr[i][j] > max should be arr[i][j] < max. E. The return type of the method should be void instead of int.

A. The initial value of max should be set to Integer.MIN_VALUE instead of 0.

Consider the following code segment. int[][] arr = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}}; int total = 0; for (int[] j : arr) { for (int k = 1; k < j.length; k++) { total += j[k]; } } What is the value of total as a result of executing the code segment? A. 66 B. 54 C. 45 D. 60 E. 0

B. 54

Consider the following code segment. String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; System.out.println(letters[1][2] + letters[2][1] + letters[1][1]); What is printed as a result of executing this code segment? A. BDA B. FHE C. HFE D. CEB E. FHI

B. FHE

Consider the following code segment. int[][] arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; int value = 0; for (int[] j : arr) { if (j[0] % 2 == 0) { for (int i = 0; i < j.length; i += 2) { j[i] = value; value++; } } } System.out.println(arr[2][2]); What is printed as a result of running this code segment? A. 8 B. 0 C. 3 D. 6 E. 5

C. 3

Consider the following method. public static int mystery(int[][] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { if ((arr[i][j] / 2) % 2 == 0) { return arr[i][j]; } } } return 0; } What does mystery return when the two-dimensional array {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}} is passed as the parameter arr? A. 2 B. 3 C. 4 D. 5 E. 6

C. 4

Consider the following method, reverse, which is intended to return the reverse the elements of arr. For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then reverse(arr) should return {{6, 5, 4}, {3, 2, 1}}. public static int[][] reverse(int[][] arr) { int[][] ret = new int[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length - 1; j++) { ret[i][j] = arr[arr.length - i - 1][arr[0].length - j - 1]; } } return ret; } The code does not work as intended. Which of the following arrays can be passed to reverse to show that the method does NOT work as intended? A. {{0}} B. {{0}, {0}} C. {{1, 0}, {1, 0}} D. {{0, 1, 0}, {0, 1, 0}} E. {{0, 1, 1, 0}, {0, 1, 1, 0}}

C. {{1, 0}, {1, 0}}

Consider the following code segment: int[][] arr = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; int[][] ret = new int[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { ret[i][j] = arr[i][arr[0].length - j - 1]; } } System.out.println(ret[2][2]); What is printed as a result of running this code segment? A. 0 B. 1 C. 2 D. 6 E. 8

D. 6

Consider the following method, which is intended to print the values in its two-dimensional integer array parameter in row-major order. public static void printArray(int[][] arr) { /* missing code */ } Consider the following code segment. int[][] arr = {{5, 4}, {3, 2}, {1, 0}}; printArray(arr); When executed, the code segment should produce the following output. 5 4 3 2 1 0 Which of the following code segments can replace /* missing code */ so that the printArray method works as intended? I. for (int[] j : arr) { for (int k : j) { System.out.print(k + " "); } } II. for (int[] j : arr) { for (int i = 0; i < j.length; i++) { System.out.print(j[i] + " "); } } III. for (int[] j : arr) { for (int k : j) { System.out.print(j[k] + " "); } } A. I only B. II only C. III only D. I and II E. I and III

D. I and II

Consider the following method, which is intended to return the element of a two-dimensional array that is closest in value to a specified number targetValue. public double findClosest(int[][] numbers, double targetValue) { int result = numbers[0][0]; int difference = Math.abs(result - targetValue); for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[0].length; col++) { if ( /* missing code */ ) { result = numbers[row][col]; difference = Math.abs(numbers[row][col] - targetValue); } } } return result; } Which of the following could be used to replace /* missing code */ so that findClosest will work as intended? A. targetValue - numbers[row][col] < difference B. Math.abs(numbers[row][col] - difference) < difference C. targetValue - numbers[row][col] < 0.0 D. Math.abs(numbers[row][col] - targetValue) < difference E. Math.abs(numbers[row][col] - targetValue) > difference

D. Math.abs(numbers[row][col] - targetValue) < difference

Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been properly declared and initialized. Which of the following can be used to print the elements in the fourth column of arr? A. System.out.print(arr[1, 4] + arr[2, 4] + arr[3, 4]); B. System.out.print(arr[0, 3] + arr[1, 3] + arr[2, 3]); C. System.out.print(arr[1][4] + arr[2][4] + arr[3][4]); D. System.out.print(arr[0][3] + arr[1][3] + arr[2][3]); E. System.out.print(arr[3][0] + arr[3][1] + arr[3][2]);

D. System.out.print(arr[0][3] + arr[1][3] + arr[2][3]);

Consider the following code segment: String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; Which statement is the correct syntax to access element "F" from the letters array? A. letters[1] B. letters[2] C. letters[1:2] D. letters[1][2] E. letters[1, 2]

D. letters[1][2]

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

E. 5

Consider the following method. public int findElement(int[][] values) { int found = values[0][0]; int result = 0; for (int[] row : values) { for (int index = 0; index < row.length; index++) { if (row[index] > found) { found = row[index]; result = index; } } } return result; } Which of the following best describes what is returned by the findElement 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. The column index of an element with the largest value in the two-dimensional array.

Consider the following code segment, where letters is a two-dimensional (2D) array that contains possible letters. The code segment is intended to print "DOG" using column-major order. String[][] letters = {{"D", "O", "G"}, {"O", "O", "-"}, {"G", "-", "G"}}; System.out.println( /* missing code */ ); Which of the following could replace /* missing code */ so that the code segment works as intended? A. letters[1][1] + letters[1][2] + letters[1][3] B. letters[0][0] + letters[0][1] + letters[0][2] C. letters[1][1] + letters[2][2] + letters[3][3] D. letters[0][0] + letters[1][1] + letters[2][2] E. letters[0][0] + letters[1][0] + letters[2][0]

E. letters[0][0] + letters[1][0] + letters[2][0]

Consider the following method, shift, which is intended to shift the elements of arr one to the left. For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then shift(arr) should return {{2, 3, 1}, {5, 6, 4}}. public static int[][] shift(int[][] arr) { int[][] ret = new int[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length - 1; j++) { ret[i][j] = arr[i][j + 1]; } } return ret; } The code does not work as intended. Which of the following arrays can be passed to shift to show that the method does NOT work as intended? A. {{0}} B. {{0}, {0}} C. {{0, 1}, {0, 1}} D. {{0, 1, 0}, {0, 1, 0}} E. {{1, 0, 0}, {1, 0, 0}}

E. {{1, 0, 0}, {1, 0, 0}}


Kaugnay na mga set ng pag-aaral

Med Surg: Patients with COPD and Asthma

View Set

Factoring Trinomials: a > 1 Quiz

View Set

Intl 6,7,8 Test & Quiz Questions

View Set

Maternity- the Postpartum Period and Associated Complications

View Set