Java OCA OCP Practice Question 3219

Question

What will be the result of attempting to run the following program?

public class Main {
  public static void main(String[] args) {
    String[][][] arr = {/*from www. ja va  2 s .co  m*/
        { {}, null },
        { { "1", "2" }, { "1", null, "3" } },
        {},
        { { "1", null } }
    };

    System.out.println(arr.length + arr[1][2].length);
  }
}

Select the one correct answer.

  • (a) The program will terminate with an ArrayIndexOutOfBoundsException.
  • (b) The program will terminate with a NullPointerException.
  • (c) 4 will be written to standard output.
  • (d) 6 will be written to standard output.
  • (e) 7 will be written to standard output.


(a)

Note

The expression arr.length will evaluate to 4.

The expression arr[1] will access the element { { "1", "2" }, { "1", null, "3" } }, and arr[1][2] will try to access the third sub-element of this element.

This produces an ArrayIndexOutOfBoundsException, since the element has only two sub-elements.




PreviousNext

Related