Traversing 2D Arrays

AP Computer Science A· difficulty 3/5

public static int run() {
    int[][] m = {{1,2,3},{4,5,6}};
    int s = 0;
    for (int[] row : m) {
        for (int v : row) {
            if (v % 2 == 1) s += v;
        }
    }
    return s;
}
// Call: System.out.println(run());

What is printed?

  • A

    12

  • B

    21

  • C

    6

  • D

    9

    check_circle

Explanation

Odd values: 1, 3, 5. Sum = 9.

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

Related questions