MCQs
Set interface extends collection interface to handle sets, which must contain
unique elements.
singletonList() returns the object as an immutable List. This is a easy way to convert
Option B is correct. A set is a collection that contains no duplicate elements. The iterator
returns the elements in no particular order (unless this set is an instance of some class
that provides a guarantee). A map cannot contain duplicate keys but it may contain duplicate values. List and Collection allow duplicate elements.
Option A is wrong. A map is an object that maps keys to values. A map cannot contain duplicate
keys; each key can map to at most one value. The Map interface provides three collection views,
which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value
mappings. The order of a map is defined as the order in which the iterators on the map's collection
views return their elements. Some map implementations, like the TreeMap class, make specific
guarantees as to their order (ascending key order); others, like the HashMap class, do not (does
not guarantee that the order will remain constant over time).
Option C is wrong. A list is an ordered collection (also known as a sequence). The user of this
interface has precise control over where in the list each element is inserted. The user can access
elements by their integer index (position in the list), and search for elements in the list. Unlike sets,
lists typically allow duplicate elements.
Option D is wrong. A collection is also known as a sequence. The user of this interface has precise
control over where in the list each element is inserted. The user can access elements by their integer
index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate
elements.
class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}
At what point is the Bar object, created on line 6, eligible for garbage collection?
Option B is correct. All references to the Bar object created on line 6 are destroyed when a
new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore
the Bar object, created on line 6, is eligible for garbage collection after line 14.
Option A is wrong. This actually protects the object from garbage collection.
Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored
in newBar on line 12. This preserver the object created on line 6.
Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.
SortedList is not a part of collection framework.
None.
All of the collection classes allow you to grow or shrink the size of your collection. ArrayList
provides an index to its elements. The newer collection classes tend not to have synchronized
methods. Vector is an older implementation of ArrayList functionality and has synchronized
methods; it is slower than ArrayList.
A constructor is a method that initializes an object immediately upon creation. It has the same
name as that of class in which it resides.
SortedList is not a part of collection framework.
None.