Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

import java.util.*;

public class Sample01
{
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10); nums.add(20); nums.add(30);
        nums.remove(1);
        System.out.println(nums);
    }
}

  • A.The program prints [10, 30]
  • B.The program prints [20, 30]
  • C.The program prints [10, 20]
  • D.The program prints [10, 20, 30]

Answer: A

There are two remove methods and the int literal 1 selects remove(int index), not remove(Object). The element at index 1 is 20, so it is taken out and the list becomes [10, 30]. Removing the VALUE 1 would have needed nums.remove(Integer.valueOf(1)).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.