TreeSet — Concept Check
Questions on TreeSet. A sorted Set backed by a TreeMap — O(log n) operations with powerful range-query methods for finding nearest elements and iterating ranges.
Questions in this quiz
7 questions on TreeSet — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a TreeSet of integers: [10, 20, 30, 40, 50]. What will set.floor(25) return?
- ANone
- B20
- C25
- D30
2.Given a TreeSet<Integer> set = new TreeSet<>(Set.of(5, 2, 8, 1, 9, 3)), what will set.first() return?
- A9
- B1
- C5
- D2
3.You use a TreeSet to store unique user IDs for sorted iteration. Which operation is O(log n)?
- AAdding a new ID
- BIterating over IDs
- CAll of the above
- DChecking if an ID exists
4.A TreeSet is used to keep track of scores. Which method finds the smallest score greater than 75?
- Ahigher(75)
- Bfloor(75)
- Cceiling(75)
- Dlower(75)
5.What will happen if you try to add a null element to a TreeSet with natural ordering?
- ANullPointerException
- BIgnored silently
- CAdded at the end
- DAdded as the first element
6.You need to maintain a sorted collection of unique values and perform range queries. Which is more suitable, TreeSet or HashSet?
- ANeither is suitable
- BHashSet
- CBoth are equally suitable
- DTreeSet
7.When using a TreeSet, what determines if two elements are considered duplicates?
- Aequals() method
- BhashCode() value
- CMemory address
- DcompareTo() result