Review Questions

graphics/rq_icon.gif

11.4

Which statements are true about collections?

Select the two correct answers.

  1. Some operations on a collection may throw an UnsupportedOperationException.

  2. Methods calling optional operations in a collection must either catch an UnsupportedOperationException or declare it in their throws clause.

  3. A List can have duplicate elements.

  4. An ArrayList can only accommodate a fixed number of elements.

  5. 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.

  1. The program will fail to compile since operations are performed on incompatible collection implementations.

  2. The program will fail to compile since the TreeSet denoted by set2 has not been given a Comparator to use when sorting its elements.

  3. The program will compile without error, but will throw an UnsupportedOperationException when run.

  4. The program will compile without error and will print all primes below 25 when run.

  5. 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.

  1. add(Object o)

  2. retainAll(Collection c)

  3. get(int index)

  4. iterator()

  5. 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.

  1. It will print [A, B, C, D, E].

  2. It will print [A, C, E].

  3. It will print [B, D, E].

  4. It will print [A, B, D].

  5. It will print [B, C, E].

  6. 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.

  1. contains()

  2. add()

  3. containsAll()

  4. retainAll()

  5. clear()



     
    ASPTreeView.com
     
    Evaluation has ѕЧДИДЙЖОЅГµexpired.
    Info...