Collections

Q: How do you invoke garbage collection to occur at a certain point? 
A: None of the above
Q: Which of the following statements is not true about the ArrayList class?
A: ArrayLists are synchronized.
Q: Which is the new method introduced in Java 8 to iterate over a collection?
A: StringList.forEach()
Q: Which of the following statements is not correct about the HashMap class?
A: The HashMap maintains the order of its elements.
Q: Which provides better performance for the insertion and removal from the middle of the list?
A: LinkedList
Q: Which allows the storage of a null key and many null values?
A: HashMap
Q: Which of these does NOT have an index-based structure?
A: Map
Q: Choose the correct option based on this program.
class Test 
{
    public static void main(String[] args) 
    {
        List  intList = new ArrayList  ();
        intList.add(10);
        intList.add(20);
        System.out.println("The list is: " + intList); 
    } 
}
A: It results in a compiler error 
Q: What will be the output of the following program?
import java.util.BitSet;
class Main {
    public static void main(String[] args) {
        BitSet obj = new BitSet(5);
        for (int i = 0; i < 5; ++i)
            obj.set(i);
        obj.clear(2);
        System.out.print(obj);
    }
}
A: {0, 1, 3, 4}
Q: What will be the output of the following program?
import java.util.*;
class Collection_iterators {
    public static void main(String args[]) {
        LinkedList list = new LinkedList();
        list.add(new Integer(2));
        list.add(new Integer(8));
        list.add(new Integer(5));
        list.add(new Integer(1));
        
        Iterator i = list.iterator();
        Collections.reverse(list);
        Collections.sort(list);
        
        while(i.hasNext())
        System.out.print(i.next() + " ");
    }
}
A: 1 2 5 8
Q: What is the output of the following program?
class Main {
    public static void main(String[] args) {
        List  intList = new LinkedList  ();
        List  dblList = new LinkedList  ();
        System.out.println("First type: " + intList.getClass());
        System.out.println("Second type:" + dblList.getClass());
    }
}
A: It prints the following: First type: class java.util.LinkedList Second type:class java.util.LinkedList 
Q: Methods such as reverse, shuffle, and sort are available in:
A: Collections
Q: When two threads access the same ArrayList object, what is the outcome of the program?
A: ConcurrentModificationException is thrown
Q: Which list class must be preferred in a multi-threading environment, considering performance constraints?
A: CopyOnWriteArrayList
Q: Which interface restricts duplicate elements?
A: Set