Traversing 2D Arrays

AP Computer Science A· difficulty 3/5

public static int run() {
    int[][] g = new int[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            g[i][j] = i * 3 + j;
    g[1][1] = g[0][2] + g[2][0];
    return g[1][1];
}
// Call: System.out.println(run());

What is printed?

  • A

    4

  • B

    8

    check_circle
  • C

    10

  • D

    6

Explanation

g[0][2] = 0<em>3+2 = 2. g[2][0] = 2</em>3+0 = 6. g[1][1] = 2 + 6 = 8.

Want 10 more like this — adaptive to your weak spots?

Related questions