What will be the result of the code:

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Test {
  public static void main(String[] args) {
    String[] str = new String[] { "1", "2", "3" };

    List list = Arrays.asList(str);
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
      Object object = (Object) iterator.next();
      iterator.remove();
    }
    System.out.println(list.size());
  }
}

Explanation
Method Arrays.asList() returns unchangeable list in which you can't add or from which you can not remove elements.
For this purpose the internal implementation of the list is used in which methods for adding or removing of the elements throws UnsupportedOperationException:

public Object remove(int i) {
  throw new UnsupportedOperationException();
}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes