Chapter 8 Multidimensional Arrays
Section 8.2 Two–Dimensional Array Basics
Section 8.2.1 Declaring Variables of Two–Dimensional Arrays and Creating Two–Dimensional Arrays
1. 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’}};
#
3. 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]
#
4. 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
#
Section 8.2.2 Obtaining the Lengths of Two–Dimensional Arrays
2. 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
#
5. How many elements are in array matrix (int[][] matrix = new int[5][5])?
a. 14
b. 20
c. 25
d. 30
#
7. 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
Key:c x.length is the number of the rows. x[0].length is the number of the elements in the first row.
#
Section 8.2.3 Ragged Arrays
6. 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.
#
8. 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
#
Section 8.3 Processing Two–Dimensional Arrays
12. 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
#
9. 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
#
11. 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
#
13. 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
#
10. 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
#
Section 8.4 Passing Two–Dimensional Arrays to Methods
14. Suppose a method p has the following heading. What return statement may be used in p()?
public static int[][] 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}};
#
15. 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
#
Section 8.8 Multidimensional Arrays
16. 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
#
18. 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
#
19. 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
#
17. 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’}}};