Sierra Bates
SCJP 6 Ch. 3 No. 3
Help me understand this question.
Obviously there is an error in the code at line 4
int[] b = (int[]) a[1];
int[] a is a 2 dimensional array and cannot be cast to the size of a one dimensional array.
But the choices given are
A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 5
F. Compilation fails due to an error on line 6
G. Compilation fails due to an error on line 7
If you choose D you are wrong - the code compiles, but an exception is thrown at runtime - so choice C is correct.
How can we tell that the error at line 4 will result in a runtime error vs. a compilation error? On the
test we will not have the option of being able to compile this code ourselves, we just have to determine the result from the code.