โ† All SCJP Flashcard Decks

Java Collections Framework Flashcards

7 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Java Collections Framework flashcards as text
  1. What happens when you call `TreeMap.subMap(fromKey, toKey)` and then try to insert a key outside that range into the returned view?

    Answer: An IllegalArgumentException is thrown

    Inserting a key outside the range of a subMap view throws IllegalArgumentException because the view enforces its boundary constraints.

  2. Which statement is true about `LinkedHashMap`?

    Answer: It maintains insertion order by default

    LinkedHashMap maintains insertion order (or optionally access order) by using a doubly-linked list alongside the hash table.

  3. Given `List list = Arrays.asList("a", "b", "c");`, what happens when you call `list.add("d")`?

    Answer: An UnsupportedOperationException is thrown

    Arrays.asList() returns a fixed-size list backed by the array; structural modifications like add() throw UnsupportedOperationException.

  4. Which collection class is best suited for implementing a LIFO (Last-In-First-Out) stack in modern Java?

    Answer: java.util.ArrayDeque

    ArrayDeque is the preferred stack implementation because it is faster than Stack (which is synchronized) and more memory-efficient than LinkedList.

  5. What is the contract between `equals()` and `hashCode()` that must be satisfied for objects used as HashMap keys?

    Answer: If a.equals(b) is true, a.hashCode() must equal b.hashCode()

    The contract requires that equal objects must have the same hash code; unequal objects may share hash codes (collisions are allowed).

  6. Which interface does `PriorityQueue` implement that defines its ordering behavior?

    Answer: Queue

    PriorityQueue implements the Queue interface; ordering is determined by the natural order of elements or a Comparator passed at construction.

  7. When iterating over a `HashMap` with an `Iterator` and you remove an element using `map.remove(key)` (not `iterator.remove()`), what occurs?

    Answer: A ConcurrentModificationException is thrown on the next iterator call

    Structurally modifying a HashMap while iterating it via a fail-fast iterator (without using iterator.remove()) causes ConcurrentModificationException.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy