Java OCA OCP Practice Question 594

Question

Which line of code causes an ArrayIndexOutOfBoundsException?

String[][] matrix = new String[1][2]; 
matrix[0][0] = "A";  // m1 
matrix[0][1] = "B";  // m2 
matrix[1][0] = "C";  // m3 
matrix[1][1] = "D";  // m4 
  • A. m1
  • B. m2
  • C. m3
  • D. m4


C.

Note

This code creates a two-dimensional array of size 1 by 2.

Lines m1 and m2 assign values to both elements in the outer array.

Line m3 attempts to reference the second element of the outer array.

Since there is no such position, it throws an exception, and Option C is correct.




PreviousNext

Related