AP Comp Sci A Unit 8 2D Arrays MCQ (ApClassroom)
Consider the following code segment, where num is an integer variable. int[][] arr = {{11, 13, 14 ,15}, {12, 18, 17, 26}, {13, 21, 26, 29}, {14, 17, 22, 28}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.print(j + k + arr[j][k] + " "); } } } What is printed when num has the value 14 ?
16 17
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?
16 times
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 four corner elements of arr ?
System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);
Consider the following code segment. int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {3, 2, 1}}; for (int j = 0; j < arr.length; j++) { for (int k = j; k < arr[0].length; k++) { System.out.print(arr[j][k] + " "); } System.out.println(); } What output is printed when the code segment is executed?
1 2 3 5 6 9
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?
5
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?
6 times
/* 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?
String[][] things
Consider the following code segment. String[][] arr = {{"Hello,", "Hi,", "Hey,"}, {"it's", "it is", "it really is"}, {"nice", "great", "a pleasure"}, {"to", "to get to", "to finally"}, {"meet", "see", "catch up with"}, {"you", "you again", "you all"}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (k == 1) { System.out.print(arr[j][k] + " "); } } } What, if anything, is printed when the code segment is executed?
Hi, it is great to get to see you again
boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ?
boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;
public boolean checkIndexes(double[][] data, int row, int col) { int numRows = data.length; if (row < numRows) { int numCols = data[0].length; return col < numCols; } else { return false; } } Consider the following variable declaration and initialization, which appears in a method in the same class as checkIndexes. double[][] table = new double[5][6]; Which of the following method calls returns a value of true ?
checkIndexes(table, 4, 5)
Consider the following code segment, where nums is a two-dimensional (2D) array of integers. The code segment is intended to print "test1234". System.out.print("test" + nums[0][0] + nums[1][0] + nums[1][1] + nums[0][1]); Which of the following code segments properly declares and initializes nums so that the code segment works as intended?
int[][] nums = {{1, 4}, {2, 3}};
String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; System.out.println( /* missing code */ ); Which of the following could replace /* missing code */ so that the code segment works as intended?
letters[1][0] + letters[2][2] + letters[2][0]
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?
num = 8
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?
{{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}
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?
{{5, 10, 15, 20}, {25, 30, 35, 40}}