Multiple choice

What is the output of the given program?

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

  1. 1 null

  2. Null null

  3. 2 an ArrayIndexOutofbound Exception is thrown

  4. ArrayIndexOutofbound Exception is thrown

  5. Compilation fails

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

This is the correct answer. The first println statement, System.out.println(array [2][1]), works fine. It selects the element/array with index 2, {0, 2,4}, and from this array it selects the element with index 1 which is 2. So, 2 gets printed. The second println statement, System.out.println(array) [2][4]), fails. It selects the array/element with index 2,{0, 2, 4}, 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.