2D Array Traversal and Summation
What is the value of sum after the code segment is executed?
Refer to the following code segment.
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] % 3 == 0) {
matrix[i][j] /= 3;
}
sum += matrix[i][j];
}
}
System.out.println(sum);
What is the value of sum after the code segment is executed?
A
50
B
54
C
56
D
58
APFIVE