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

B.

Null element 0
Null element 1

C.

Null
Null

D. A NullPointerException is thrown at runtime.

题解

程序第 6 行创建了长度为 2 的字符串数组,数组的每个元素会自动被初始化为「null」。

程序第 9 行使用到字符串对象的「concat」方法,但由于变量所存的参考为 null,因此会发生 NullPointerException。