Multiple choice

What is the output of the given code?

Int [] [] array = {{0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16}};
Systemout.printIn(array [4] [1]);
System.out.printIn (array) [1][4]);

  1. 4 null

  2. Null 4

  3. An exception is thrown at run time.

  4. 4 an ArrayIndexOutofbound exception is thrown.

  5. Compilation fails

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

This is the correct answer. The first print in statement, system.out.println(array [4][1]), works fine. It selects the element/array with index 4, {0, 4, 8, 12, 16} and from this array, it selects the element with index 1, which is 4. So, 4 gets printed. The second print in statement, system.out.println(array) [1][4]) fails. It selects the array/element with index 1,{0, 1} and from this array, it tries to select the element with index 4. This causes an exception. So, an exception in thread main java.lang.ArrayIndexOutOfBoundsException occurs.