Java OCA OCP Practice Question 2539

Question

Consider the following program:

import java.util.*;

public class Main {
        public static void main(String []args) {
                String hello = "hello";
                String world = "world";
                StringBuffer helloWorld = new StringBuffer(hello + world);
                List<String> list =
                        Arrays.asList(hello, world, helloWorld.toString());
                helloWorld.append("!");
                list.remove(0);        // REMOVE
                System.out.println(list);
        }//from  w  w w .j a  v  a  2s .co m
}

Which one of the following options is correct?

  • a)When compiled, this program will result in a compiler error in linked marked with comment REMOVE.
  • b)When run, this program will crash with throwing the exception UnsupportedOperationException when executing the line marked with comment REMOVE.
  • c)When run, this program will print the following output: [hello, world, helloworld]
  • d)When run, this program will print the following output: [world, helloworld!]
  • e)When run, this program will print the following output: [world, helloworld]


b)

Note

The Arrays.asList() method returns a List object that is backed by a fixed-length array.

You cannot modify the List object returned by this array, so calling methods such as add() or remove() will result in throwing an UnsupportedOperationException.




PreviousNext

Related