Given:
public static void main(String[] args) {
String theString = "Hello World";
System.out.println(theString.charAt(11));
}
What is the result?
A. The program prints nothing
B.
d
C. A StringIndexOutOfBoundsException is thrown at runtime.
D. An ArrayIndexOutOfBoundsException is thrown at runtime.
E. A NullPointerException is thrown at runtime.
題解
theString變數所參考到的字串為長度是11的「Hello World」,程式第7行要取得「Hello World」索引位置為11的字元,會超出字串長度,因此會拋出StringIndexOutOfBoundsException例外。