LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Predict the Output · Question 16

List.remove with an int versus an Integer Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 16 Back to Exercise

What is the output of the following Java program?

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

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(List.of(10, 20, 30, 1, 2));
        list.remove(1);                        // remove(int index)
        list.remove(Integer.valueOf(1));        // remove(Object o)
        System.out.println(list);
    }
}
  • A [10, 30, 2] Correct
  • B [10, 20, 30, 2]
  • C [20, 30, 1, 2]
  • D [10, 30, 1, 2]
Correct Answer: Option A

remove(1) is the int overload, removing index 1 (the 20); remove(Integer) removes the value 1. Overloading prefers the exact primitive match over boxing.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links