What is the main characteristic of a TreeMap?
A
Maintains insertion order B
Maintains sorted order of keys C
Allows duplicate keys D
Uses hash-based structure
Analysis & Theory
`TreeMap` keeps keys sorted in natural order or based on a custom comparator.
Which package contains TreeMap?
A
java.tree B
java.map C
java.util D
java.sort
Analysis & Theory
`TreeMap` is part of the `java.util` package.
What is the time complexity for `get()` and `put()` operations in TreeMap?
A
O(1) B
O(n) C
O(log n) D
O(n log n)
Analysis & Theory
TreeMap operations are O(log n) as it is implemented using a Red-Black tree.
Which of the following is NOT true about TreeMap?
A
It allows one null key B
It stores keys in sorted order C
It is thread-safe D
It does not allow duplicate keys
Analysis & Theory
`TreeMap` is not synchronized by default; it is not thread-safe.
What happens if keys added to a TreeMap are not mutually comparable?
A
They are inserted in random order B
TreeMap ignores the keys C
TreeMap throws ClassCastException D
They are added normally
Analysis & Theory
`TreeMap` requires keys to be comparable; otherwise, it throws `ClassCastException`.
Which method retrieves the first (lowest) key?
A
getMinKey() B
firstKey() C
getFirst() D
minKey()
Analysis & Theory
`firstKey()` returns the lowest key in the map.
Which method returns a view of the portion of the map whose keys are strictly less than a given key?
A
lowerMap() B
headMap(key) C
tailMap(key) D
floorMap(key)
Analysis & Theory
`headMap(key)` gives a view of the portion with keys less than the specified key.
What does `descendingMap()` do?
A
Sorts values in reverse B
Returns a reversed key-value map view C
Clears the TreeMap D
Removes the last entry
Analysis & Theory
`descendingMap()` returns a reverse-ordered view of the TreeMap.
Which method removes all mappings from a TreeMap?
A
removeAll() B
clear() C
reset() D
empty()
Analysis & Theory
`clear()` removes all entries from the TreeMap.
Which interface does TreeMap implement?
A
SortedMap B
NavigableMap C
Map D
All of the above
Analysis & Theory
`TreeMap` implements `Map`, `SortedMap`, and `NavigableMap` interfaces.