Java · Question 10 · Medium

What will be the output of the following Java program?

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30, 40));
        list.remove(1);
        list.remove(Integer.valueOf(40));
        System.out.println(list);
    }
}
  1. [20, 30]
  2. [10, 20, 30]
  3. [30, 40]
  4. [10, 30]
Answer and explanation

Answer: D. [10, 30]

Explanation: list.remove(1) calls remove(int index) and removes the element at index 1 (20). remove(Integer.valueOf(40)) calls remove(Object) and removes the value 40.

Previous: What will be the output of the following Java program? · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge