2D Array Traversal Error
What runtime error occurs in this code and why?
double[][] prices = {{0.99, 1.29, 1.49}, {0.79, 0.99}};
for(int i = 0; i < prices.length; i++){
for(int j = 0; j < prices[0].length; j++){
System.out.print(prices[i][j] + " ");
}
System.out.println();
}
A
ArrayIndexOutOfBoundsException occurs because prices[0].length is used for all rows, but the second row has fewer elements.
B
NullPointerException due to uninitialized elements.
C
No error; it correctly prints all elements.
D
Compilation error due to mismatched row lengths.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE