Unit 8 - AP Computer Science - AP Classroom

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

int[][] multi = new int[4][4]; for (int rows = 0; rows < 4; rows++) { for (int cols = 0; cols < 4; cols++) { if (cols == 0) { multi[rows][cols] = 0; } else if (cols == 1) { multi[rows][cols] = 1; } else if (cols == 2) { multi[rows][cols] = 2; } if ((rows % 2 == 0) && (cols % 2 == 0)) { if ((rows >= 2) && (cols <= 2)) { multi[rows][cols] = 9; } } } } As a result of executing the code segment, how many elements in the two-dimensional (2D) array multi will store the value 9 ?

C. 2

Consider the following code segment, which is intended to display "cat". String[][] keyboard = {{"q", "w", "e", "r", "t"}, {"a", "s", "d", "f", "g"}, {"z", "x", "c", "v", "b"}}; System.out.println(/* missing expression */); Which of the following can replace /* missing expression */ so that the code segment works as intended?

keyboard[2][2] + keyboard[1][0] + keyboard[0][4]

A two-dimensional array myArray is to be created with the following contents. {{0, 0, 3}, {0, 0, 0}, {7, 0, 0}} Which of the following code segments can be used to correctly create and initialize myArray ? int myArray[][] = new int[3 [3]; myArray[0][2] = 3; myArray[2][0] = 7; int myArray[][] = new int[3][3];myArray[0][2] = 7;myArray[2][0] = 3; int myArray[][] = {{0, 0, 3}, {0, 0, 0}, {7, 0, 0}};

D. I and III

int[][] arr = {{1, 3, 4}, {4, 5, 3}}; int max = arr[0][0]; for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { int temp = arr[row][col]; if (temp % 2 == 0) { arr[row][col] = temp + 1; // line 11 } if (temp > max) { max = temp; } } } System.out.println(max); How many times will the statement in line 11 be executed as a result of executing the code segment?

2

Consider the following code segment. int[][] arr = {{3, 2, 1}, {4, 3, 5}}; for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { if (col > 0) { if (arr[row][col] >= arr[row][col - 1]) { System.out.println("Condition one"); } } if (arr[row][col] % 2 == 0) { System.out.println("Condition two"); } } } As a result of executing the code segment, how many times are "Condition one" and "Condition two" printed?

"Condition one" is printed once, and "Condition two" is printed twice.

Consider the following code segment. int[][] mat = {{10, 15, 20, 25}, {30, 35, 40, 45}, {50, 55, 60, 65}}; for (int[] row : mat) { for (int j = 0; j < row.length; j += 2) { System.out.print(row[j] + " "); } System.out.println(); } What, if anything, is printed as a result of executing the code segment?

10 20 30 40 50 60

Consider the following code segment, where num is a properly declared and initialized integer variable. The following code segment is intended to set foundRow and foundCol to the row and column indexes of an array element containing num. The code segment does not work as intended. int[][] arr = {{10, 11, 12, 13}, {22, 24, 26, 28}, {15, 16, 17, 18}, {40, 41, 42, 43}}; int foundRow = -1; int foundCol = -1; for (int j = 0; j < arr.length; j++) { for (int k = 1; k < arr[0].length; k++) { if (arr[j][k] == num) { foundRow = j; foundCol = k; } } } Which of the following values for num can be used as a test case to show that the code segment does not work as intended?

15

Consider the following code segment, where twoD is a two-dimensional (2D) String array. The code segment is intended to display "JAVA". System.out.print(twoD[2][1]); System.out.print(twoD[3][2]); System.out.print(twoD[1][1]); Which of the following code segments properly declares and initializes twoD so that the code segment works as intended?

String[][] twoD = {{"V", "AV", "J"}, {"JA", "VA", "A"}, {"JA", "J", "JAV"}, {"AV", "V", "A"}};

Consider the following code segment, which is intended to create and initialize the two-dimensional (2D) integer array num so that columns with an even index will contain only even integers and columns with an odd index will contain only odd integers. int[][] num = /* missing code */; Which of the following initializer lists could replace /* missing code */ so that the code segment will work as intended?

{{0, 1, 2}, {4, 5, 6}, {8, 3, 6}}

Consider the following method, sumRows, which is intended to traverse all the rows in the two-dimensional (2D) integer array num and print the sum of all the elements in each row. public static void sumRows(int[][] num) { for (int[] r : num) { int sum = 0; for (int j = 0; j < num.length; j++) { sum += r[j]; } System.out.print(sum + " "); } } For example, if num contains {{3, 5}, {6, 8}}, then sumRows(num) should print "8 14 ". The method does not always work as intended. For which of the following two-dimensional array input values does sumRows NOT work as intended?

{{4, 1, 7}, {-10, -11, -12}}


Ensembles d'études connexes

Unit 2: Life/Health Insurance Underwriting QBank

View Set

buying using and disposing ch 10

View Set

Anatomy and physiology of the lactating breast

View Set

Chapter 1 :Perspective on Maternal , Newborn , and womens health

View Set

Life Insurance - D. Life Insurance and Annuities - Policy Replacement/Cancellation

View Set

HESI MILESTONE 2 PRACTICE QUESTION

View Set

Chapter 13- Monopolistic Competition

View Set

Chapter 35 Pediatric Emergencies:

View Set