2D Arrays: Creation and Access

AP Computer Science A· difficulty 3/5

int[][] m = new int[3][2];
int sum = 0;
for (int r = 0; r < m.length; r++) {
    for (int c = 0; c < m[r].length; c++) {
        sum += m[r][c];
    }
}
System.out.println(sum);

What is printed?

  • A

    0

    check_circle
  • B

    5

  • C

    6

  • D

    Garbage value

Explanation

Default int values are 0, so all 6 cells contain 0 and the sum is 0.

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

Related questions

AP Computer Science A · 2D Arrays: Creation and Access Practice Question | Acemy