| preferred AP College board partner for AP classes
AP Computer Science A/Unit 8: 2D Array
Start Practice TestPractice Test
Share
About Exam
medium Solved by 3 students
2D Array Traversal Error
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy