Java Questions 110 -120  «Prev  Next»


hashCode(Java Questions)

  1. Should you use transient variables in your hashCode() implementations?

    Answer:
    Transient variables can interfere with your equals() and hashCode() implementations. A transient variable is a variable that cannot be serialized.

  2. What are 2 basic operations you will use with collections?

    Answer:
    1. Add objects to the collection
    2. Remove objects from the collection

  3. What are the core interfaces you need to know for the exam?

    Answer:
    Collections, List, Queue, Set, Map, Navigable Set, Sorted Set, Sorted Map, Navigable Map

  4. What are the 2 kinds of Utilities?

    Answer:
    Collections and Arrays.

  5. Do all collections pass the IS-A test for Collection?

    Answer:
    No. None of the Map-related classes and interfaces extend from Collection.



  6. From which interface are SortedMap, Hashtable, HashMap, NavigableMap, LinkedHashMap, and TreeMap derived from?

    Answer:
    The Map interface.

  7. What are the 3 overloaded uses of the word collection?

    Answer:
    1. Collection: Represents any of the data structures in which objects are stored and iterated over
    2. Collection: java.util.Collection interface, from which Set, List, and Queue extend
    3. Collections: java.util.Collections contains static utility methods for use with collections
    The Map interface in Java is a member of the java.util package and is a part of the Java Collections Framework. It is different from other Collection interfaces such as List and Set in several ways:
    1. A Map is an interface that maps keys to values. It does not store elements like List and Set, but rather stores key-value pairs.
    2. A Map does not allow duplicate keys, but it can have duplicate values.
    3. The Map interface does not extend the Collection interface, but it does extend the Iterable interface. This means that a Map can be iterated over, but it does not have all of the methods that a Collection has, such as add() and remove().
    4. The Map interface has specific methods for getting, putting, and removing key-value pairs, such as get(), put(), and remove().

    Here is an example of how you might use the Map interface in Java:
    Map <String, Integer> map = new HashMap <>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("three", 3);
    
    int value = map.get("two");
    // value is now 2
    
    map.remove("three");
    
    if (map.containsKey("three")) {
        // this code will not be reached because the key "three" was removed
    }
    

  8. What kind of data type is collections?

    Answer:
    Collections is a class [Static Utility Methods]. Collection is an interface with declarations of the methods common to most collections including
    1. add
    2. remove
    3. contains
    4. size
    5. iterator

  9. What are the four types of Collections?

    Answer:
    1. L - Lists
    2. M - Maps
    3. Q - Queues
    4. S - Sets

  10. How should one think of sorting?

    Answer:
    Sorting is a specific type of ordering.