Given:
public class Test {
public static void main(String[] args) {
Integer num = Integer.parseInt(args[1]);
System.out.println("Number is : " + num);
}
}
And the commands:
javac Test.java
java Test 12345
java Test 12345
What is the result?
A.
Number us : 12345
B. A NullPointerException is thrown at runtime
C. A NumberFormatException is thrown at runtime
D. An ArrayIndexOutOfBoundException is thrown at runtime.
題解
題目執行的指令會編譯「Test.java」,由於「java」指令指定的類別是 Test,因此會執行 Test 類別的 main 方法,並把「12345」作為 main 的參數。參數的索引值從 0 開始。
程式第 4 行要取得參數索引 1 的值,但是這裡的參數數量只有一個,因此會超出陣列索引範圍,而拋出 ArrayIndexOutOfBoundsException 例外。
