Practice mode Java

What is the output of the Java code List<Integer> x = new ArrayList<>(List.of(10, 20, 30)); x.remove(1); System.out.println(x);?

Correct answer A. [10, 30]

Explanation

List has both remove(int index) and remove(Object o). The argument 1 is an int, so overload resolution chooses remove(int) without any boxing, and the element at index 1, which is 20, is removed. Passing Integer.valueOf(1) would select remove(Object), find no match and leave the list unchanged.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.