2D Array Traversal Error
What is the result when the following code segment is executed?
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
Compilation error due to mismatched row lengths.
B
ArrayIndexOutOfBoundsException occurs because prices[0].length is used for all rows, but the second row has fewer elements.
C
NullPointerException due to uninitialized elements.
D
No error; it correctly prints all elements.
Question Leaderboard
Not enough data yet to show leaderboard.
