| 11.4 | Which statements are true about collections? Select the two correct answers. -
Some operations on a collection may throw an UnsupportedOperationException . -
Methods calling optional operations in a collection must either catch an UnsupportedOperationException or declare it in their throws clause. -
A List can have duplicate elements. -
An ArrayList can only accommodate a fixed number of elements. -
The Collection interface contains a method named get . | | 11.5 | What will be the result of attempting to compile and run the following program? import java.util.*; public class Sets { public static void main(String[] args) { HashSet set1 = new HashSet(); addRange(set1, 1); ArrayList list1 = new ArrayList(); addRange(list1, 2); TreeSet set2 = new TreeSet(); addRange(set2, 3); LinkedList list2 = new LinkedList(); addRange(list2, 5); set1.removeAll(list1); list1.addAll(set2); list2.addAll(list1); set1.removeAll(list2); System.out.println(set1); } static void addRange(Collection col, int step) { for (int i = step*2; i<=25; i+=step) col.add(new Integer(i)); } } Select the one correct answer. -
The program will fail to compile since operations are performed on incompatible collection implementations . -
The program will fail to compile since the TreeSet denoted by set2 has not been given a Comparator to use when sorting its elements. -
The program will compile without error, but will throw an UnsupportedOperationException when run. -
The program will compile without error and will print all primes below 25 when run. -
The program will compile without error and will print some other sequence of numbers when run. | | 11.6 | Which of these methods are defined in the Collection interface? Select the three correct answers. -
add(Object o) -
retainAll(Collection c) -
get(int index) -
iterator() -
indexOf(Object o) | | 11.7 | What will be the output from the following program? import java.util.*; public class Iterate { public static void main(String[] args) { List l = new ArrayList(); l.add("A"); l.add("B"); l.add("C"); l.add("D"); l.add("E"); ListIterator i = l.listIterator(); i.next(); i.next(); i.next(); i.next(); i.remove(); i.previous(); i.previous(); i.remove(); System.out.println(l); }; }; Select the one correct answer. -
It will print [A, B, C, D, E]. -
It will print [A, C, E]. -
It will print [B, D, E]. -
It will print [A, B, D]. -
It will print [B, C, E]. -
It will print throw a NoSuchElementException . | | 11.8 | Which of these methods from the Collection interface will return the value true if the collection was modified during the operation? Select the two correct answers. -
contains() -
add() -
containsAll() -
retainAll() -
clear() | |