2D Array Traversals MCQ Practice

Question 1 of 10

A stamp club prints sheet values in row-major order. What text does this Java code display?

int[][] sheet = { {1, 2}, {3, 4} };
for (int r = 0; r < sheet.length; r++) {
    for (int c = 0; c < sheet[0].length; c++) {
        System.out.print(sheet[r][c]);
    }
}
  1. A. 1234
  2. B. 1324
  3. C. 2413
  4. D. 13

Question 2 of 10

A seedling flat is printed in column-major order. What text does this Java code display?

int[][] flat = { {1, 2}, {3, 4} };
for (int c = 0; c < flat[0].length; c++) {
    for (int r = 0; r < flat.length; r++) {
        System.out.print(flat[r][c]);
    }
}
  1. A. 1234
  2. B. 1324
  3. C. 3412
  4. D. 24

Question 3 of 10

A choir prints riser tags with nested enhanced-for loops. What text does this Java code display?

int[][] risers = { {8, 1, 6}, {3, 5, 7} };
for (int[] row : risers) {
    for (int tag : row) {
        System.out.print(tag);
    }
}
  1. A. 816
  2. B. 836157
  3. C. 816357
  4. D. 357816

Question 4 of 10

A puzzle club assigns to the inner enhanced-for variable. What text does this Java code display?

int[][] tiles = { {2, 5}, {1, 8} };
for (int n : tiles[0]) {
    n = 0;
}
System.out.print(tiles[0][0]);
  1. A. 0
  2. B. 1
  3. C. 5
  4. D. 2

Question 5 of 10

A parking garage prints each aisle's length using the outer enhanced-for variable. What text does this Java code display?

int[][] aisles = { {4, 9, 1}, {2, 8, 6} };
for (int[] aisle : aisles) {
    System.out.print(aisle.length);
}
  1. A. 2
  2. B. 36
  3. C. 33
  4. D. 6

Question 6 of 10

A weather buoy log ends each row with println. What text does this Java code display?

int[][] log = { {2, 0}, {7, 4} };
for (int r = 0; r < log.length; r++) {
    for (int c = 0; c < log[0].length; c++) {
        System.out.print(log[r][c]);
    }
    System.out.println();
}
  1. A. 2074
  2. B. 20 74
  3. C. 27 04
  4. D. 2 0 7 4

Question 7 of 10

A marquee reprints letters from the last row upward. What text does this Java code display?

int[][] letters = { {2, 5, 9}, {1, 8, 4} };
for (int r = letters.length - 1; r >= 0; r--) {
    for (int c = 0; c < letters[0].length; c++) {
        System.out.print(letters[r][c]);
    }
}
  1. A. 259184
  2. B. 952481
  3. C. 214859
  4. D. 184259

Question 8 of 10

A cargo bay is visited column-major. Which value is printed first?

row Column 0 Column 1 Column 2
Row 0 5 8 3
Row 1 2 9 4
int[][] bay = { {5, 8, 3}, {2, 9, 4} };
for (int c = 0; c < bay[0].length; c++) {
    for (int r = 0; r < bay.length; r++) {
        System.out.print(bay[r][c]);
    }
}
  1. A. 5
  2. B. 8
  3. C. 2
  4. D. 3

Question 9 of 10

A lantern row variable is reassigned inside an enhanced-for. What text does this Java code display?

int[][] lanterns = { {2, 5}, {1, 8} };
for (int[] row : lanterns) {
    row = new int[] {0, 0};
}
System.out.print(lanterns[0][0] + "," + lanterns[1][0]);
  1. A. 0,0
  2. B. 0,1
  3. C. 2,1
  4. D. 2,0

Question 10 of 10

A seed vault writes through the outer enhanced-for row. What text does this Java code display?

int[][] vault = { {2, 5}, {1, 8} };
for (int[] row : vault) {
    row[0] = 0;
}
System.out.print(vault[0][0] + "," + vault[1][0]);
  1. A. 2,1
  2. B. 2,0
  3. C. 0,1
  4. D. 0,0