Given:

import java.util.ArrayList;
import java.util.List;

public class Whizlabs{
    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(21); list.add(15);
        list.add(30); list.add(11);
        list.add(2);
        //insert here
        System.out.println(list);
    }
}

Which inserted at line 11, will provide the following output?

[21, 15, 11]

A.

list.removeIf(e -> e % 2 != 0);
B.
list.removeIf(e -> e % 2 == 0);
C.
list.remove(e -> e % 2 == 0);
D. None of the above.

题解

选项 A,会移除整数集合中,所有不能整除 2 的项目。所以集合中的元素会只剩下「30」和「2」。

选项 B,会移除整数集合中,所有能整除 2 的项目。所以集合中的元素会剩下「21」、「15」和「11」。

选项 C,remove 方法的用法错误。

选项 D,由于选项 B 就是答案,因此错误。