Core Java - Collections - Interview Questions and Answers

Revision as of 08:04, 13 June 2018 by Rasimsen (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What happens when you compile and run the below program?

package com.javatutorial.quiz;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Quiz38 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        Integer[] arr = {2,10,3};
        list = Arrays.asList(arr);
        list.set(0, 3);


[3,10,3], followed by exception

 Arrays.asList() returns a fixed-size list backed by the specified array. Therefore, the arraylist can't grow.
So, when add() is called, an exception is thrown.