Java Topic Practice
2D Array Traversal
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
2D Array Traversal MCQ Practice
2D Array Traversal Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - 2D Array Traversal: track the final printed value.
int[][] grid = {
{2, 5, 1},
{4, 3, 6},
{7, 0, 8}
};
String out = "";
for (int c = 0; c < grid[0].length; c++) {
for (int r = 0; r < grid.length; r++) {
out += grid[r][c] + " ";
}
}
System.out.print(out.trim());All 15 2d array traversal questions
Work through the interactive quiz above first. This is the full 2d array traversal question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - 2D Array Traversal: track the final printed value.
int[][] grid = { {2, 5, 1}, {4, 3, 6}, {7, 0, 8} }; String out = ""; for (int c = 0; c < grid[0].length; c++) { for (int r = 0; r < grid.length; r++) { out += grid[r][c] + " "; } } System.out.print(out.trim());- 8 6 1 0 3 5 7 4 2
- 2
- 2 4 7 5 3 0 1 6 8
- 2 5 1 4 3 6 7 0 8
Show worked solution
Correct answer: 2 4 7 5 3 0 1 6 8
Putting the column loop on the outside walks the grid column by column instead of row by row.
2.Code Tracing 12 - 2D Array Traversal: follow the variable updates.
int[][] grid = { {4, 7, 3}, {6, 5, 8}, {9, 2, 10} }; int total = 0; for (int r = 0; r < grid.length; r++) { total += grid[r][r]; } System.out.print(total);- 54
- 14
- 23
- 19
Show worked solution
Correct answer: 19
Using the same variable for both subscripts walks the main diagonal of the grid.
3.Code Tracing 28 - 2D Array Traversal: trace the branch and loop path.
int[][] grid = { {7, 6, 1}, {6, 8, 11}, {7, 1, 13} }; int best = 0; int bestSum = 0; for (int r = 0; r < grid.length; r++) { int sum = 0; for (int c = 0; c < grid[r].length; c++) sum += grid[r][c]; if (r == 0 || sum > bestSum) { bestSum = sum; best = r; } } System.out.print(best + ":" + bestSum);- 25:1
- 0:14
- 2:21
- 1:25
Show worked solution
Correct answer: 1:25
The inner loop resets its accumulator on every row, so each row total is independent.
4.Code Tracing 39 - 2D Array Traversal: watch the index changes.
int[][] grid = { {9, 8, 3}, {5, 10, 7}, {9, 3, 15} }; int count = 0; for (int[] row : grid) { for (int v : row) { if (v % 2 == 0) count++; } } System.out.print(count);- 2
- 7
- 9
- 3
Show worked solution
Correct answer: 2
The outer enhanced for loop hands back each row as an int array, which the inner loop then walks.
5.Code Tracing 50 - 2D Array Traversal: evaluate the state change step by step.
int[][] grid = { {11, 6, 5}, {4, 12, 9}, {11, 1, 17} }; int total = 0; for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if ((r + c) % 2 == 0) total += grid[r][c]; } } System.out.print(total);- 57
- 56
- 20
- 76
Show worked solution
Correct answer: 56
Testing the parity of r + c selects a checkerboard pattern of cells rather than whole rows or columns.
6.What is printed by the following code segment?
int[][] g = {{1, 2, 3}, {4, 5, 6}}; String out = ""; for (int c = 0; c < g[0].length; c++) { for (int r = 0; r < g.length; r++) { out += g[r][c]; } } System.out.print(out);- 123456
- 412536
- 654321
- 142536
Show worked solution
Correct answer: 142536
Putting the column loop outside walks the grid column by column.
7.What is printed by the following code segment?
int[][] g = {{1, 2}, {3, 4}, {5, 6}}; int t = 0; for (int r = 0; r < g.length; r++) { int rs = 0; for (int c = 0; c < g[r].length; c++) { rs += g[r][c]; } if (rs % 2 == 1) { t += rs; } } System.out.print(t);- 21
- 0
- 11
- 3
Show worked solution
Correct answer: 21
Every row total is odd here, so all three are added.
8.What is printed by the following code segment?
int[][] g = {{1, 0}, {0, 1}}; int c = 0; for (int r = 0; r < g.length; r++) { for (int k = 0; k < g[r].length; k++) { if (g[r][k] == 1 && r == k) { c++; } } } System.out.print(c);- 1
- 2
- 4
- 0
Show worked solution
Correct answer: 2
Only the diagonal cells hold 1, and both satisfy the combined condition.
9.What is printed by the following code segment?
int[][] g = {{1, 2, 3}, {4, 5, 6}}; int t = 0; for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { if ((r + c) % 2 == 0) { t += g[r][c]; } } } System.out.print(t);- 12
- 21
- 9
- 10
Show worked solution
Correct answer: 9
The cells whose row and column indexes sum to an even number hold 1, 3, and 5.
10.What is printed by the following code segment?
int[][] g = new int[3][3]; for (int i = 0; i < 3; i++) { g[i][2 - i] = 1; } int t = 0; for (int[] row : g) { for (int v : row) { t += v; } } System.out.print(t);- 1
- 9
- 0
- 3
Show worked solution
Correct answer: 3
One cell per row is marked along the opposite diagonal.
11.What is printed by the following code segment?
int[][] g = {{2, 4}, {6, 8}}; for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { g[r][c] /= 2; } } System.out.print(g[0][0] + "" + g[1][1]);- 14
- 28
- 13
- 24
Show worked solution
Correct answer: 14
Every cell is halved in place, and the two reported cells become 1 and 4.
12.What is printed by the following code segment?
int[][] g = {{1, 2}, {3, 4}}; int[][] h = new int[2][2]; for (int r = 0; r < 2; r++) { for (int c = 0; c < 2; c++) { h[c][r] = g[r][c]; } } System.out.print(h[0][1] + " " + h[1][0]);- 2 3
- 3 2
- 1 4
- 4 1
Show worked solution
Correct answer: 3 2
Swapping the indexes on the write transposes the grid.
13.What is printed by the following code segment?
int[][] g = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int t = 0; for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { if (r == 0 || r == g.length - 1 || c == 0 || c == g[r].length - 1) { t += g[r][c]; } } } System.out.print(t);- 45
- 30
- 40
- 20
Show worked solution
Correct answer: 40
Every cell except the centre lies on the border, so the total is 45 minus 5.
14.What is printed by the following code segment?
int[][] g = {{1, 2}, {3, 4}}; String out = ""; for (int r = g.length - 1; r >= 0; r--) { for (int c = g[r].length - 1; c >= 0; c--) { out += g[r][c]; } } System.out.print(out);- 1234
- 3412
- 2143
- 4321
Show worked solution
Correct answer: 4321
Both loops count down, so the grid is visited in fully reversed order.
15.What is printed by the following code segment?
int[][] g = {{1, 1}, {1, 1}}; int c = 0; boolean stop = false; for (int r = 0; r < g.length && !stop; r++) { for (int k = 0; k < g[r].length && !stop; k++) { if (g[r][k] == 1) { c++; } if (c == 3) { stop = true; } } } System.out.print(c);- 3
- 4
- 2
- 1
Show worked solution
Correct answer: 3
A flag tested in both loop conditions stops the traversal cleanly as soon as the count reaches three.
