LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Exercise 1 of 4

HashMap and TreeMap HashMap and TreeMap MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on Java maps: HashMap, LinkedHashMap and TreeMap, their methods, iteration, internals, null keys, navigation and LRU caches. Pick an option to check your answer instantly.

Question 11 Easy

By default, in what order does LinkedHashMap iterate?

A Sorted by key
B Insertion order
C Random
D Sorted by value
Question page & discussion
Question 12 Hard

Which method do you override in LinkedHashMap to evict old entries automatically?

A removeOldest()
B removeEldestEntry(Map.Entry)
C evict()
D trimToSize()
Question page & discussion
Question 13 Medium

What does counts.merge(word, 1, Integer::sum) do?

A Always sets the count to 1
B Inserts 1 if the word is absent, otherwise adds 1 to the existing count
C Merges two maps
D Removes the word
Question page & discussion
Question 14 Medium

What does groups.computeIfAbsent(k, x -> new ArrayList<>()).add(v) do?

A Replaces the list each time
B Creates a list the first time k is seen and adds v to the list for k
C Throws if k exists
D Adds k to every list
Question page & discussion
Question 15 Easy

Which is the most efficient way to iterate over both keys and values?

A for (K k : map.keySet()) map.get(k)
B for (Map.Entry<K, V> e : map.entrySet())
C for (V v : map.values())
D for (int i = 0; i < map.size(); i++)
Question page & discussion
Question 16 Hard

A mutable object is used as a HashMap key and a field used in its hashCode is changed afterwards. What happens?

A The map updates automatically
B The entry usually cannot be found any more, because it sits in the bucket for the old hash
C An exception is thrown
D The key is removed
Question page & discussion
Question 17 Medium

Which statement about Hashtable is true?

A It allows a null key
B It is a legacy synchronized class that allows no null keys or values
C It keeps keys sorted
D It is faster than HashMap
Question page & discussion
Question 18 Medium

Which is true of Map.of("a", 1, "b", 2)?

A It can be modified
B It is immutable, rejects null keys/values and duplicate keys, and its iteration order is unspecified
C It keeps insertion order
D It is a TreeMap
Question page & discussion
Question 19 Medium

Which map should several threads update concurrently?

A HashMap
B ConcurrentHashMap
C TreeMap
D LinkedHashMap
Question page & discussion
Question 20 Hard

What happens if you remove an element from map.values()?

A Nothing – it is a copy
B The corresponding entry is removed from the map, because values() is a live view
C An exception is always thrown
D Only the value becomes null
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links