2D Array Traversals MCQ Practice

Question 1 of 10

A library bin sheet is printed row-major. What text does this Java code display?

int[][] bins = { {5, 6, 7}, {8, 9, 0} };
for (int r = 0; r < bins.length; r++) {
    for (int c = 0; c < bins[0].length; c++) {
        System.out.print(bins[r][c]);
    }
}
  1. A. 567
  2. B. 586970
  3. C. 890567
  4. D. 567890

Question 2 of 10

The same bins are printed column-major. What text does this Java code display?

int[][] bins = { {5, 6, 7}, {8, 9, 0} };
for (int c = 0; c < bins[0].length; c++) {
    for (int r = 0; r < bins.length; r++) {
        System.out.print(bins[r][c]);
    }
}
  1. A. 567890
  2. B. 586970
  3. C. 596870
  4. D. 58

Question 3 of 10

A clay shelf is walked with nested enhanced-for loops. What text does this Java code display?

int[][] shelf = { {9, 2}, {4, 8}, {1, 6} };
for (int[] row : shelf) {
    for (int n : row) {
        System.out.print(n);
    }
}
  1. A. 924816
  2. B. 941286
  3. C. 928416
  4. D. 981246

Question 4 of 10

A bakery assigns to each muffin count inside enhanced-for. What text does this Java code display?

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

Question 5 of 10

A nest-box map prints the first column through the outer enhanced-for row. What text does this Java code display?

int[][] nests = { {4, 7}, {8, 3}, {1, 6} };
for (int[] row : nests) {
    System.out.print(row[0]);
}
  1. A. 471
  2. B. 48136
  3. C. 736
  4. D. 481

Question 6 of 10

A ferry seating chart prints each row, then a newline. What text does this Java code display?

int[][] seats = { {1, 4, 2}, {8, 5, 3} };
for (int r = 0; r < seats.length; r++) {
    for (int c = 0; c < seats[0].length; c++) {
        System.out.print(seats[r][c]);
    }
    System.out.println();
}
  1. A. 142853
  2. B. 182 453
  3. C. 142 853
  4. D. 1 4 2 8 5 3

Question 7 of 10

A quilt is printed right-to-left on every row. What text does this Java code display?

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

Question 8 of 10

A tide-pool grid is visited column-major. Which value is printed second?

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

Question 9 of 10

A poster row is replaced only in the loop variable. What text does this Java code display?

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

Question 10 of 10

A cider house writes through each enhanced-for row. What text does this Java code display?

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