What is the result?

Given the code fragment:
<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]);
int [] [] array = {{0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16}};
System.out.println(array [4][1]);
System.out.println(array) [1][4]);
</code>
What is the result?

Given the code fragment:

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]);
int [] [] array = {{0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16}};
System.out.println(array [4][1]);
System.out.println(array) [1][4]);

What is the result?

A.
4 Null

B.
Null 4

C.
An IllegalArgumentException is thrown at run time

D.
4 An ArrayIndexOutOfBoundException is thrown at run time

Explanation:

The first println 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, 4. Output: 4
The second println statement, System.out.println(array) [1][4]);, fails. It selects the array/element
with index 1, {0, 1}, and from this array it try to select the element with index 4. This causes an
exception.
Output:
4
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Alex

Alex

I think there is a typo for this question. In fact the latest three rows are equal to the first three. It is clear that so written the code can’t compile. Instead , if you cut them then the right answer is D.

sully

sully

yeah answer is D but need a . on the first print statement after System

Vuur

Vuur

There would be several compiler errors:

1) No such type as Int
2) No class named Systemout
3) Parentheses messed up in “System.out.printIn (array) [1][4]);”
4) Double declaration of “array”
5) Parentheses messed up in “System.out.println(array) [1][4]);”