Tuesday 24 July 2012

Objects and Collections - Objective Questions - Part 2

1)

You need to store elements in a collection that guarantees that no duplicates are stored.
Which one of the following interfaces provide that capability?

A. Java.util.Map
B. Java.util.List
C. Java.util.Collection
D. None of the above

Answer: A

Option A is correct. A Map cannot contain duplicate keys.
Option B is wrong. Lists typically allow duplicate elements.
Option C is wrong. Collection allows duplicate elements.

2)

Which interface does java.util.Hashtable implement?

A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.Collection

Answer: A

Hash table based implementation of the Map interface. This implementation provides all of the optional map
operations

3)

What will be the output of the program?

TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext() )
{
    System.out.print( it.next() + " " );
}

A. one two three four
B. four three two one
C. four one three two
D. one two three four one

Answer: C

TreeSet assures no duplicate entries; also, when it is accessed it will return elements in natural order, which
typically means alphabetical.

4)

Which statement is true for the class java.util.HashSet?

A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. e elements in the collection are accessed using a unique key.

Answer: C

Option C is correct. HashSet implements the Set interface and the Set  interface specifies collection that
contains no duplicate elements.
Option A is wrong. HashSet makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
Option B is wrong. The set can be modified.
Option D is wrong. This is a Set and not a Map.

5)

Which statement is true for the class java.util.ArrayList?

A. The elements in the collection are ordered.
B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique.
D. The elements in the collection are accessed using a unique key.

Answer: A

Yes, always the elements in the collection are ordered.

6)

Suppose that you would like to create an instance of a new Map that has an iteration order
that is the same as the iteration order of an existing instance of a Map. Which concrete
implementation of the Map interface should be used for the new instance?

A. TreeMap
B. HashMap
C. LinkedHashMap
D. The answer depends on the implementation of the existing instance.

Answer: C

The iteration order of a Collection is the order in which an iterator moves through the elements of the
Collection. The iteration order of a LinkedHashMap is determined by the order in which elements are inserted.When a new LinkedHashMap is created by passing a reference to an existing Collection to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be invoked.

7)

You need to store elements in a collection that guarantees that no duplicates are stored.
Which interface provides that capability?

A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.Collection

Answer: B

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.

8)

What will be the output of the program?

import java.util.*;
class I
{
    public static void main (String[] args)
    {
        Object i = new ArrayList().iterator();
        System.out.print((i instanceof List)+",");
        System.out.print((i instanceof Iterator)+",");
        System.out.print(i instanceof ListIterator);
    }
}

A. Prints: false, false, false
B. Prints: false, false, true
C. Prints: false, true, false
D. Prints: false, true, true

Answer: C

The iterator() method returns an iterator over the elements in the list in proper sequence, it doesn't return
a List or a ListIterator object.A ListIterator can be obtained by invoking the listIterator method.

9)

What two statements are true about properly overridden hashCode() and equals() methods?

   1. hashCode() doesn't have to be overridden if equals() is.
   2. equals() doesn't have to be overridden if hashCode() is.
   3. hashCode() can always return the same value, regardless of the object that invoked it.
   4. equals() can be true even if it's comparing different objects.

A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 3

Answer: C

3) and (4) are correct.(1) and (2) are incorrect because by contract hashCode() and equals() can't be overridden unless both are overridden.

10)

Which two statements are true about equals() and hashCode() methods have been properly
overridden?

   1. If the equals() method returns true, the hashCode() comparison == must return true.
   2. If the equals() method returns false, the hashCode() comparison != must return true.
   3. If the hashCode() comparison == returns true, the equals() method must return true.
   4. If the hashCode() comparison == returns true, the equals() method might return true.

A. 1 and 4
B. 2 and 3
C. 3 and 4
D. 1 and 3

Answer: A

(1) is a restatement of the equals() and hashCode() contract. (4) is true because if the hashCode() comparison returns ==, the two objects might or might not be equal.(2) and (3) are incorrect because the hashCode() method  is very flexible in its return values, and often two dissimilar objects can return the same hash code value.

No comments:

Post a Comment