Recursive 2D Array Traversal
What is the output when the program is executed?
public class TwoDArraySum {
public static int sum(int[][] arr, int i, int j) {
if (i == arr.length) return 0;
if (j == arr[i].length) return sum(arr, i + 1, 0);
return arr[i][j] + sum(arr, i, j + 1);
}
public static void main(String[] args) {
int[][] matrix = { {1, 2, 3}, {4, 5, 6} };
System.out.println(sum(matrix, 0, 0));
}
}
A
20
B
21
C
19
D
Compilation Error
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE