Programming Fundamentals Chapter 8 Quiz

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

9

Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; int sum = 0; for (int i = 0; i < array.length; i++) sum += array[i][0]; System.out.println(sum);

6 5 4 3 2 1

Show the output of the following code: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; for (int i = array.length - 1; i >= 0; i--) { for (int j = array[i].length - 1; j >= 0; j--) System.out.print(array[i][j] + " "); System.out.println(); }

1 8

Show the output of the following code: int[][][] array = {{{1, 2}, {3, 4}}, {{5, 6},{7, 8}}}; System.out.println(array[0][0][0]); System.out.println(array[1][1][1]);

int[][][] m = new int[4][6][5];

Declare an array variable for a three-dimensional array, create a 4 × 6 × 5 int array, and assign its reference to the variable.

arrayVar[rowIndex][columnIndex].

Each element in a two-dimensional array is represented using the syntax:

new elementType [ROW_SIZE][COLUMN_SIZE]

A two-dimensional array can be created using the syntax:

elementType[][] arrayVar.

A variable for two-dimensional arrays can be declared using the syntax:

True

An element in a two-dimensional array is accessed through a row and column index.

The program runs and displays x[2][2] is false.

Analyze the following code: public class Test { public static void main(String[] args) { boolean[][] x = new boolean[3][]; x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3]; System.out.println("x[2][2] is " + x[2][2]); } } A. The program has a compile error because new boolean[3][] is wrong. B. The program has a runtime error because x[2][2] is null. C. The program runs and displays x[2][2] is null. D. The program runs and displays x[2][2] is true. E. The program runs and displays x[2][2] is false.

120 x.length is 12 x[2].length is 5 x[0][0].length is 2

Assume char[][][] x = new char[12][5][2], how many elements are in the array? What are x.length, x[2].length, and x[0][0].length?

4 and 5

Assume double[][] x = new double[4][5], what are x.length and x[2].length? A. 4 and 4 B. 4 and 5 C. 5 and 4 D. 5 and 5

4, 5, and 6

Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length? A. 4, 5, and 6 B. 6, 5, and 4 C. 5, 5, and 5 D. 4, 5, and 4

2, 3, and 4

Assume int[][] x = {{1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length? A. 2, 3, and 3 B. 2, 3, and 4 C. 3, 3, and 3 D. 3, 3, and 4 E. 2, 2, and 2

3 and 2

Assume int[][] x = {{1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length? A. 2 and 1 B. 2 and 2 C. 3 and 2 D. 2 and 3 E. 3 and 3

Yes. They are ragged/jagged array.

Can the rows in a two-dimensional array have different lengths?

True

Data in a table or a matrix can be represented using a two-dimensional array.

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

Declare an array reference variable for a two-dimensional array of int values, create a 4 × 5 int matrix, and assign it to the variable.

25

How many elements are in array matrix (int[][] matrix = new int[5][5])? A. 14 B. 20 C. 25 D. 30

2 4

Show the output of the following code: public class Test { public static void main(String[] args) { int[][] array = {{1, 2, 3, 4}, {5, 6, 7, 8}}; System.out.println(m1(array)[0]); System.out.println(m1(array)[1]); } public static int[] m1(int[][] m) { int[] result = new int[2]; result[0] = m.length; result[1] = m[0].length; return result; } }

return new int[][]{{1, 2, 3}, {2, 4, 5}};

Suppose a method p has the following heading: public static int[][] p() What return statement may be used in p()? A. return 1; B. return {1, 2, 3}; C. return int[]{1, 2, 3}; D. return new int[]{1, 2, 3}; E. return new int[][]{{1, 2, 3}, {2, 4, 5}};

a[0][0]

What is the index variable for the element at the first row and first column in array a? A. a[0][0] B. a[1][1] C. a[0][1] D. a[1][0]

2 5 9 13

What is the output of the following code? public class Test { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[i][1] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

5

What is the output of the following code? public class Test { public static void main(String[] args) { int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; System.out.print(data[1][0][0]); } } A. 1 B. 2 C. 4 D. 5 E. 6

4

What is the output of the following code? public class Test { public static void main(String[] args) { int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; System.out.print(ttt(data[0])); } public static int ttt(int[][] m) { int v = m[0][0]; for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) if (v < m[i][j]) v = m[i][j]; return v; } } A. 1 B. 2 C. 4 D. 5 E. 6

4 5 6 7

What is the output of the following code? public class Test5 { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " "); } } A. 1 2 3 4 B. 4 5 6 7 C. 1 3 8 12 D. 2 5 9 13 E. 3 6 10 14

array[0][1] is 2.

What is the output of the following code? int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]);

The program prints two rows 1 3 4 5 followed by 1 2 6 33

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { java.util.Arrays.sort(values[row]); for (int column = 0; column < values[row].length; column++) System.out.print(values[row][column] + " "); System.out.println(); } } } A. The program prints two rows 3 4 5 1 followed by 33 6 1 2 B. The program prints on row 3 4 5 1 33 6 1 2 C. The program prints two rows 3 4 5 1 followed by 2 1 6 33 D. The program prints two rows 1 3 4 5 followed by 1 2 6 33 E. The program prints one row 1 3 4 5 1 2 6 33

5 33

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { System.out.print(m(values[row]) + " "); } } public static int m(int[] list) { int v = list[0]; for (int i = 1; i < list.length; i++) if (v < list[i]) v = list[i]; return v; } } A. 3 33 B. 1 1 C. 5 6 D. 5 33 E. 33 5

33

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int row = 0; row < values.length; row++) for (int column = 0; column < values[row].length; column++) if (v < values[row][column]) v = values[row][column]; System.out.print(v); } } A. 1 B. 3 C. 5 D. 6 E. 33

1

What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}}; int v = values[0][0]; for (int[] list : values) for (int element : list) if (v > element) v = element; System.out.print(v); } } A. 1 B. 3 C. 5 D. 6 E. 33

True

When you create an array using the following statement, the element values are automatically initialized to 0. int[][] matrix = new int[5][5]; A. True B. False

char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

Which of the following statements are correct? A. char[][] charArray = {'a', 'b'}; B. char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}}; C. char[2][] charArray = {{'a', 'b'}, {'c', 'd'}}; D. char[][] charArray = {{'a', 'b'}, {'c', 'd'}};

char[][][] charArray = new char[2][2][]; char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};

Which of the following statements are correct? A. char[][][] charArray = new char[2][2][]; B. char[2][2][] charArray = {'a', 'b'}; C. char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}; D. char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};

int[][] y = new int[3][]; int[][] z = {{1, 2}}; int[][] m = {{1, 2}, {2, 3}};

Which of the following statements are valid? int[][] r = new int[2]; int[] x = new int[]; int[][] y = new int[3][]; int[][] z = {{1, 2}}; int[][] m = {{1, 2}, {2, 3}}; int[][] n = {{1, 2}, {2, 3}, };

: elementType[][] arrayVar = {{row values}, . . . , {row values}}.

You can create and initialize a two-dimensional array using an array initializer with the syntax:

elementType[][][] arrayVar new elementType[size1][size2] [size3]

You can use arrays of arrays to form multidimensional arrays. For example, a variable for three-dimensional arrays can be declared as ____________________, and a three-dimensional array can be created using ____________________.


Conjuntos de estudio relacionados

Educational Psychology Chapter 14

View Set

QuickBooks Certification 2018-Questions

View Set