NEW
Font size
WorksheetsUnit 5 - Two-Dimensional Arrays
Total questions: 19
Worksheet time: 31mins
Consider the code to the left.
What is printed out as a result of executing this code?
H
L
M
N
ArrayIndexOutOfBounds
Consider the following code segment, where letters is a two-dimensional (2D) array that contains possible letters. The code segment is intended to print "PIG".
String[][] letters = { {"A", "B", "C", "D"},
{"E", "F", "G", "H"},
{"I", "J", "K", "L"},
{"M", "N", "O", "P"}};
System.out.println( /* missing code */ );
Which of the following could replace /* missing code */ so that the code segment works as intended?
letters[4][4] + letters[3][1] + letters[2][3]
letters[3][3] + letters[2][0] + letters[1][2]
letters[4][3] + letters[3][0] + letters[2][2]
letters[3][3] + letters[0][2] + letters[2][1]
letters[4][4] + letters[1][3] + letters[3][2]
Consider the following two-dimensional array words.
String[][] words = { {"about", "actor", "admit", "agile"},
{"badge", "basic", "berry", "bingo"},
{"cacti", "cedar", "chair", "close"}};
What is the fifth element visited when row-major traversal is performed on words?
about
agile
badge
basic
cedar
Consider the Code segment to the left:
What is printed when num has the value 5?
4 1 3
5 6 8
6 7 9
8 5 7
9 6 8
Consider the Code segment to the left:
What is printed when num has the value 5?
4 1 3
5 6 8
6 7 9
8 5 7
9 6 8
Consider the following two-dimensional array words.
String[][] words = { {"about", "actor", "admit", "agile"},
{"badge", "basic", "berry", "bingo"},
{"cacti", "cedar", "chair", "close"}};
What is the fifth element visited when column-major traversal is performed on words?
about
agile
badge
basic
cedar
Consider the code to the left:
Which of the following is the reason why an exception is thrown when the code is executed?
The row index is out of range because the for loop on line 5 iterates one time too many.
The column index is out of range because the for loop on line 6 iterates one time too many.
The column index is out of range because the indexes are in the wrong order.
The row index is out of range because the indexes are in the wrong order.
Consider the method, reverse, to the left:
Which is intended to return the reverse the elements of arr. For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then reverse(arr) should return {{6, 5, 4}, {3, 2, 1}}.
What choice could replace /* missing statement */ so that reverse works as intended?
ret[row][col];
ret[arr.length - row - 1][arr[0].length - col - 1]
arr[arr.length - row - 1][arr[0].length - col - 1]
ret[arr.length - row][arr[0].length - col]
arr[arr.length - row][arr[0].length - col]
Consider to the left the method, reverse, which is intended to return the reverse the elements of arr. For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then reverse(arr) should return {{6, 5, 4}, {3, 2, 1}}.
The code does not work as intended. Which of the following arrays can be passed to reverse to
show that the method does NOT work as intended?
{{0}}
{{0}, {0}}
{{0, 1}, {0, 1}}
{{0, 0}, {1, 1}, {0, 0}}
{{0, 0}, {1, 1}, {1, 1}, {0, 0}}
Consider the following code segment.
int[][] arr = {{0, 0, 0}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
for (int[] row : arr) {
for (int item : row) {
System.out.print(item + " ");
}
System.out.println(" ");
}
How many times will the statement System.out.print(item + " ") be executed?
0
3
4
9
12
Consider the code to the left:
What is the value of total as a result of executing the code segment?
0
30
36
44
66
Consider the method to the left:
Which code segment could replace /*MISSING CODE*/ for the method to work as intended?
p = pixels[r][c];
max = p.getRed();
max = pixels[r][c].getRed();
return p;
max = p.getRed();
p = pixels[r][c];
max = pixels[r][c].getRed();
return p;
The method findMax is intended to return the largest element in the array of positive numbers called numbers. The method may not work as intended.
Which of the following changes, if any, can be made to line 6 so that the method will work as intended?
Change numbers[row][col] < max to numbers[row][col] == max.
Change numbers[row][col] < max to numbers[row][col] != max.
Change numbers[row][col] < max to numbers[row][col] <= max.
Change numbers[row][col] < max to numbers[row][col] > max.
No change is necessary; the method works correctly as is.
Consider the method to the left:
Which of the following best describes what the method does?
Modifies the total number of Pixels in the array to be 255
Counts the number of Pixels in the given array that are equal to 255
Finds and returns the brightest Pixel in the given array
Changes all of the Pixels in the given array to the color white
This method doesn’t do anything because you cannot modify an object with an enhanced for loop.
Consider the following two-dimensional array definition.
int[][] data = {{1, 4, 3, 7}, {2, 6, 1, 9}, {6, 5, 9, 8}};
Consider the code segment to the left, where all elements in data have been initialized.
What is the output of this code segment?
4
5
0 1
4 2 6
4 2 6 6 8
Consider the code to the left:
Which array below, when used as an argument, would cause the hasDuplicates method to return false?
{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
{{-4, -3, -2}, {-1, 0, 1}, {2, 3, 4}}
{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
{{6, 1, 3}, {7, 2, 5}, {6, 8, 9}}
{{5, 6, 7}, {5, 8, 9}, {5, 0, 1}}
Consider the following two-dimensional array.
int[][] data = new int[3][5];
Consider the following code segment. Assume all elements in data have been initialized.
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[0].length; col++) {
if (row == col) {
System.out.println(data[row][col]);
}
}
}
How many times is the println statement called when the code segment is executed?
0
3
5
8
15
Consider the method, (to the left) shift, which is intended to shift the elements of arr one to the left. For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then shift(arr) should return {{2, 3, 1}, {5, 6, 4}}.
What choice could replace /* missing statement */ so that shift works as intended?
ret[i][j] = arr[i][j - 1];
ret[i][j] = arr[i][j + 1];
arr[i][j] = ret[i][j - 1];
arr[i][j] = ret[i][j + 1];
arr[i][j] = ret[i][j];
Consider the following method, (to the left)shift, which is intended to shift the elements of arr one to the left.
For example, if arr contains {{1, 2, 3}, {4, 5, 6}}, then shift(arr) should return {{2, 3, 1}, {5, 6, 4}}.
The code does not work as intended. Which of the following arrays can be passed to shift to
show that the method does NOT work as intended?
{{0}}
{{0}, {0}}
{{0, 1}, {0, 1}
{{0, 0, 1}, {0, 0, 1}}
{{0, 0, 0, 1}, {0, 0, 0, 1}}
