String[] strs = new String[2];
int idx = 0;
for (String s : strs) {
strs[idx].concat(" element " + idx);
idx++;
}
for (idx = 0; idx < strs.length; idx++) {
System.out.println(strs[idx]);
}
What is the result?
A.
Element 0
Element 1
Element 1
B.
Null element 0
Null element 1
Null element 1
C.
Null
Null
Null
D. A NullPointerException is thrown at runtime.
題解
程式第 6 行建立了長度為 2 的字串陣列,陣列的每個元素會自動被初始化為「null」。
程式第 9 行使用到字串物件的「concat」方法,但由於變數所存的參考為 null,因此會發生 NullPointerException。
