QuizzesJava TreeMap & Sorting
intermediate
java

Java TreeMap & Sorting

Master TreeMap, sorted maps, Comparable, Comparator, Collections.sort, and custom ordering in Java.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java TreeMap & Sorting. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is a TreeMap in Java?

    • AA Map that groups entries by a tree structure of categories
    • BA Map that stores entries in a binary tree for fast insertion
    • CA SortedMap implementation backed by a red-black tree, always sorted by key
    • DA thread-safe sorted HashMap
  2. 2.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Map<String, Integer> map = new TreeMap<>();
            map.put("banana", 2);
            map.put("apple", 5);
            map.put("cherry", 1);
            System.out.println(map);
        }
    }
    • A{cherry=1, banana=2, apple=5}
    • BOrder is not guaranteed
    • C{apple=5, banana=2, cherry=1}
    • D{banana=2, apple=5, cherry=1}
  3. 3.What interface must a class implement to be used as a TreeMap key without a custom Comparator?

    • ACloneable
    • BSerializable
    • CComparator
    • DComparable
  4. 4.What is the difference between Comparable and Comparator?

    • AComparable defines the natural order inside the class; Comparator defines external custom ordering
    • BComparable supports multiple sort orders; Comparator supports only one
    • CComparable is for sorting lists; Comparator is for sorting maps
    • DComparator is faster than Comparable
  5. 5.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<String> names = Arrays.asList("Charlie", "Alice", "Bob");
            Collections.sort(names);
            System.out.println(names);
        }
    }
    • A[Charlie, Bob, Alice]
    • B[Charlie, Alice, Bob]
    • C[Bob, Alice, Charlie]
    • D[Alice, Bob, Charlie]
  6. 6.How do you sort a list in descending order?

    • ACollections.sort(list, "desc")
    • BCollections.reverseSort(list)
    • CCollections.sort(list, false)
    • Dlist.sort(Comparator.reverseOrder())
  7. 7.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<String> words = Arrays.asList("banana", "fig", "apple", "kiwi", "date");
            words.sort(Comparator.comparingInt(String::length));
            System.out.println(words);
        }
    }
    • A[fig, date, kiwi, apple, banana]
    • B[apple, banana, date, fig, kiwi]
    • C[fig, kiwi, date, apple, banana]
    • D[banana, apple, kiwi, date, fig]
  8. 8.What does Comparator.thenComparing() do?

    • AReverses the comparison result
    • BChains two lists together for sorting
    • CSorts by two fields simultaneously
    • DApplies a secondary sort when the primary comparator considers two elements equal
  9. 9.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            TreeMap<Integer, String> map = new TreeMap<>();
            map.put(3, "three");
            map.put(1, "one");
            map.put(4, "four");
            map.put(2, "two");
            System.out.println(map.firstKey());
            System.out.println(map.lastKey());
        }
    }
    • A3 4
    • B1 4
    • C1 2
    • D3 2
  10. 10.How do you create a TreeMap sorted in descending key order?

    • Anew TreeMap<>().reverse()
    • BTreeMap does not support custom ordering
    • Cnew TreeMap<>(Comparator.reverseOrder())
    • Dnew TreeMap<>(false)
  11. 11.What is the time complexity of put() and get() in a TreeMap?

    • AO(1)
    • BO(n)
    • CO(n log n)
    • DO(log n)
  12. 12.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            TreeMap<Integer, String> map = new TreeMap<>();
            for (int i = 1; i <= 5; i++) map.put(i * 10, "v" + i);
            System.out.println(map.floorKey(35));
            System.out.println(map.ceilingKey(35));
        }
    }
    • A40 30
    • Bnull null
    • C35 35
    • D30 40
  13. 13.What is a stable sort?

    • AA sort with O(n log n) guaranteed complexity
    • BA sort that always produces the same result regardless of input
    • CA sort that preserves the relative order of equal elements
    • DA sort that never throws exceptions
  14. 14.What does this code print?

    import java.util.*;
    public class Main {
        record Person(String name, int age) {}
        public static void main(String[] args) {
            List<Person> people = new ArrayList<>(List.of(
                new Person("Charlie", 30),
                new Person("Alice", 25),
                new Person("Bob", 25)
            ));
            people.sort(Comparator.comparingInt(Person::age)
                                  .thenComparing(Person::name));
            people.forEach(p -> System.out.println(p.name()));
        }
    }
    • AAlice Charlie Bob
    • BCharlie Alice Bob
    • CBob Alice Charlie
    • DAlice Bob Charlie
  15. 15.What is the difference between TreeMap and TreeSet?

    • ATreeMap is faster; TreeSet is slower
    • BTreeMap stores key-value pairs; TreeSet stores only unique sorted values
    • CTreeSet allows duplicates; TreeMap does not
    • DTreeSet is backed by a TreeMap internally — they are identical
  16. 16.What does Arrays.sort() use internally for primitive arrays?

    • AHeap sort
    • BQuick sort
    • CMerge sort
    • DDual-pivot quicksort
  17. 17.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(5, 3, 8, 1, 9, 2);
            int max = Collections.max(nums);
            int min = Collections.min(nums);
            System.out.println(max + " " + min);
        }
    }
    • A8 2
    • B5 3
    • C9 1
    • D1 9
  18. 18.When should you use TreeMap instead of HashMap?

    • AAlways — TreeMap is faster
    • BWhen the map will have fewer than 10 entries
    • CWhen you need keys in sorted order or range queries like floorKey/ceilingKey/subMap
    • DWhen you need null keys
  19. 19.What does this code print?

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            TreeMap<String, Integer> map = new TreeMap<>();
            map.put("apple", 1);
            map.put("apricot", 2);
            map.put("banana", 3);
            map.put("cherry", 4);
            System.out.println(map.subMap("apricot", "cherry"));
        }
    }
    • A{apricot=2, banana=3, cherry=4}
    • B{apricot=2, banana=3}
    • C{apple=1, apricot=2, banana=3, cherry=4}
    • D{apple=1, apricot=2, banana=3}
  20. 20.What sorting algorithm does Collections.sort() use for Object lists?

    • AHeap sort
    • BQuicksort
    • CTimSort
    • DMerge sort