Which of the following exception will be thrown due to the statement given here?



int array[] = new int[-2];

A. NullPointerException
B. NegativeArraySizeException
C. ArrayIndexOutOfBoundsException
D. IndexOutOfBoundsException
E. This statement does not cause any exception.

題解

由於在實體化陣列時定義的陣列長度小於0,因此會拋出NegativeArraySizeException例外。