Java Certification Questions 121 -130  «Prev  Next»


hashCode() and hashSet implementations (Java)

  1. Do HashSets guarantee any ordering?

    Answer:
    Java HashSet does not guarantee any insertion orders of the set but it allows null elements.

  2. What must be true in order for a collection to be sorted?

    Answer:
    Whenever you want a collection to be sorted its elements must be mutually comparable.

  3. What must be true when you use a class that implements Map?

    Answer:
    Any classes you use as a part of the keys for that map must override the 1) hashCode() and 2) equals methods.

  4. What does the PriorityQueue method "offer" do?

    Answer:
    The "offer" method is used to add elements to the PriorityQueue.

  5. What does the subSet method do with respect to TreeSets?

    Answer:
    subSet(s, b*, e, b*)
    
    Returns a subset starting at element s and ending just before element e. [SCJP P590 By K.S.]

  6. With respect to TreeMaps what does the method "higherkey" do?

    Answer:
    TreeMap.higherKey(key)
    
    Return the lowest key which is greater than the key.

  7. What do the methods 1) poll and 2) peak do with respect to Queues?

    Answer:
    The remove() and poll() methods remove and return the head of the queue.
    Exactly which element is removed from the queue is a function of the queue's ordering policy, which differs from implementation to implementation.
    The element() and peek() methods return, but do not remove, the head of the queue.

  8. Which method does the Comparable interface have to implement?

    Answer:
    The Comparable interface has to implement the compareTo() method.

  9. Which method does the Comparator interface use?

    Answer:
    The Comparator interface makes use of the compare() method.

  10. Which 3 methods exist in Queues which do not exist in other collections?

    Answer:
    1. peek()
    2. poll()
    3. offer()