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

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」,由於Test類別有用public修飾,因此預設會執行Test類別的main方法,並把「12345」作為main的參數。參數的索引值從0開始。

程式第4行要取得參數索引1的值,但是這裡的參數數量只有一個,因此會超出陣列索引範圍,而拋出ArrayIndexOutOfBoundException例外。