Unit 8 practice

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

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

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 ?

2

Given the code, what is the value of things.length? double [ ] [ ] things = { { 1.2, 9.0, 8.7, 1.0}, {9.2, 0.5, 0.0, 5.2}, {7.3, 7.9, 1.2, 3.9}}; .length determines number of rows

3

What is the value of things.length? { 1, 9, 4, 3} { 6, 5, 0, 2} { 7, -2 ,10 ,15}

3

What is the value of things[2].length? { 1, 9, 4, 3} { 6, 5, 0, 2} { 7, -2 ,10 ,15}

4

What statement returns a[1].length return? int [ ] [ ] a= { { 2, 4, 6, 8}, { 1, 2, 3, 4}};

4

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?

String[][] things

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"}};

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]);

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.

What is in values [2][1] { 12, -9, 8} { 7, 14, 21} { -32, -1, 0}

-1

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

What is the output? public void test() { int [][] nums = new int [4] [5]; int x = 9; modifier(nums, x); System.out.println(nums[0][0] + " " + x); } public void modifier (int [] [] values, int n){ values[0][0] = 5; n++; }

5 9

Given the code, what is in values [2][1] double [ ] [ ] values = { { 1.2, 9.0, 3.2}, {9.2, 0.5, -1.2}, {7.3, 7.9, 4.8}}; row 2, column 1

7.9

What value is at index 1 in this array? String[] names = {"Mack", "Dennis", "Dee", "Charlie"};

Dennis

True or False: An array can be store different types of data (like store both doubles and ints).

False

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 ? I. int[][] myArray = new int[3][3];myArray[0][2] = 3; myArray[2][0] = 7; II. int[][] myArray = new int[3][3]; myArray[0][2] = 7; myArray[2][0] = 3; III. int[][] myArray = {{0, 0, 3}, {0, 0, 0}, {7, 0, 0}};

I and III

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 ?

boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

Consider the following method. 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)

Which of the following correctly checks if an integer value column(which represents a column position shockingly) is within the boundaries of a 2D array value?

column >= 0 && column <= values[0].length

Which command below would create the array pictured below correctly? { 12, -9, 8} { 7, 14, 21} { -32, -1, 0}

double [ ] [ ] values = { {12, -9, -8}, {7, 14, 21}, {-32, -1, 0} };

Given the code, which of the following fragments prints out every element of items? int [ ] [ ] items = { { 0, 1, 3, 4}, { 4, 3, 99, 0}, { 3, 2, 7, 70}};

for(int row = 0; row< items.length; row++) { System.out.println(); for( int col = 0; col < items[row].length; col ++) System.out.print(items [row][col] + " ") }

Which of the following statements correctly declares a two-dimensional integer array?

int Matrix[ ][ ] = new int[5][4];

For the 2D array of integers money, which command will refer to the 0th row?

int [] row = money [0];

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}};

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? Responses

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

Consider the following code segment, where letters is a two-dimensional (2D) array that contains possible letters. The code segment is intended to print "DIG". 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]

What is in values [3][0] { 12, -9, 8} { 7, 14, 21} { -32, -1, 0}

no such value

For the 2D array of Strings named seatingChart, how would you access the name in the bottom right-hand corner?

seatingChart[seatingChart.length-1][seatingChart[0].length -1]

Given the following: double [][] stuff ; Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?

stuff = new double[5][7] ;

Which of the following statements replaces 0 with 42? { 1, 9, 4, 3} { 6, 5, 0, 2} { 7, -2 ,10 ,15}

things [1] [2] = 42;

What are the contents of mat after the code has been executed. 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; } } }

{{ 2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1}}

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}}


Kaugnay na mga set ng pag-aaral

Chapter 6 Project Manager: Time Management

View Set

Cell and Transportation Mechanisms

View Set

Chapter 21 flashcards for physics

View Set

Special Senses: Vision Questions

View Set

Reporting of injuries and receiving medical treatment

View Set

Period 5 AP classroom questions

View Set

BIO 168 Chapter 11: Functional Organization of Nervous Tissue

View Set