Collections Quiz - Languages Java
Free practice quiz: Collections (Languages Java). 51 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages Java practice quiz has 51 multiple-choice questions on Collections, and 51 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 51 questions, freeSample questions
Which of the following is an immutable List implementation in Java's Collections framework?
Show answer
Answer: D. UnmodifiableList
UnmodifiableList is an interface that provides a view of a list that cannot be modified. While not a concrete class itself, implementations that wrap existing lists to make them unmodifiable (like `Collections.unmodifiableList(list)`) ensure immutability. ArrayList and LinkedList are mutable. CopyOnWriteArrayList provides thread-safe modification but is not immutable.
Which `Set` implementation does not permit duplicate elements and does not guarantee any specific order of elements?
Show answer
Answer: C. HashSet
`HashSet` uses a hash table for storage. It does not allow duplicate elements and does not maintain any order of iteration. `TreeSet` maintains sorted order, `LinkedHashSet` maintains insertion order.
Consider the code: `List<String> list = Arrays.asList("a", "b", "c"); Iterator<String> it = list.iterator(); it.next(); it.remove(); System.out.println(list);`. What will be printed?
Show answer
Answer: B. [b, c]
`Arrays.asList()` creates a fixed-size list backed by the array. The `iterator().remove()` method removes the element last returned by `next()`. So, after calling `it.next()`, "a" is the current element. Calling `it.remove()` removes "a" from the list. The list then contains ["b", "c"].
Which of the following is a `Map` interface implementation that uses a hash table and allows `null` keys and values?
Show answer
Answer: D. HashMap
`HashMap` uses a hash table and allows one `null` key and multiple `null` values. `TreeMap` does not allow `null` keys (but allows `null` values if the `Comparator` handles them). `Hashtable` does not allow `null` keys or values. `LinkedHashMap` also allows one `null` key and multiple `null` values.
What is the Big O notation for iterating through all elements of a `TreeSet` of size 'n'?
Show answer
Answer: C. O(n)
Iterating through a `TreeSet` involves traversing the underlying balanced binary search tree. Since the tree contains 'n' elements, and visiting each element once takes constant time per node, the total time complexity for iteration is O(n).
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the primary difference between `HashSet` and `TreeSet` in terms of element storage and retrieval?
- When would you choose a `LinkedHashMap` over a `HashMap`?
- Which interface in Java Collections represents a collection that is guaranteed to be sorted according to natural ordering or by a `Comparator`?
- What is the time complexity for iterating through all elements of an `ArrayList` of size 'n'?
- Which of the following is a thread-safe implementation of a `List` in Java?
- What exception is typically thrown when attempting to remove an element from an empty `Iterator` using its `remove()` method?
- Consider the following code: `Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("apple", 3); System.out.println(map.get("apple"));`. What will be printed?
- Which of the following `Queue` implementations is suitable for implementing a priority queue?
- What is the effect of calling `Collections.sort()` on a `List` containing `null` elements?
- What is the purpose of the `ListIterator` interface?
- Which of the following is true about `ArrayDeque`?
- What is the Big O notation for adding an element to the end of an `ArrayList` when the underlying array needs to be resized?
- Which method is used to retrieve and remove the head of a `Queue` without throwing an exception if the queue is empty?
- What is the primary advantage of using `EnumSet` over `HashSet` for storing enum constants?
- Which of the following `Map` implementations is suitable for maintaining the insertion order of keys?
- What is the difference between `Collections.sort(list)` and `list.sort(null)` in Java 8 and later?
- What is the primary characteristic of a `PriorityQueue` concerning its elements?
- Which method on a `Map` returns a `Set` view of the values contained in the map?
- What is the primary use case for `ConcurrentHashMap`?
- Which interface represents a collection that holds elements with a natural ordering or a specified `Comparator` and does not allow duplicate elements?
- What is the Big O notation for getting an element at a specific index 'i' from an `ArrayList`?
- Which of the following collections does NOT allow `null` elements?
- What is the main difference between `HashMap` and `TreeMap` in terms of key ordering?
- Which method should be used to retrieve and remove the head of a `Queue` if an exception is desired when the queue is empty?
- What is the Big O notation for adding an element to the beginning of a `LinkedList`?
- Which `Set` implementation maintains elements in the order they were inserted?
- What is the purpose of the `Collections.unmodifiableList()` method?
- What is the Big O notation for removing an element from the middle of an `ArrayList`?
- Which of the following is a synchronized `Map` implementation?
- What is the time complexity for removing an element from the beginning of a `LinkedList`?
- Which method returns an immutable `List` from a `Map`'s key set?
- What is the primary purpose of the `Deque` interface?
- If you need to sort a `List` of custom objects using a specific criterion not present in their natural ordering, which approach would you use?
- What is the Big O notation for checking if an element exists in a `HashSet`?
- Which `Map` implementation provides sorted keys and thread-safe access?
- What is the purpose of the `Queue` interface in Java Collections?
- Which of the following is a thread-safe, non-blocking `Queue` implementation?
- What exception is thrown by `Collections.sort()` if the list contains elements that are not mutually comparable (e.g., mixing `String` and `Integer` without a custom `Comparator`)?
- Which `Map` interface method returns a `Set` of `Map.Entry` objects, allowing access to both keys and values?
- What is the Big O notation for adding an element to the end of a `LinkedList`?
- Which of the following is a disadvantage of using `Vector` compared to `ArrayList`?
- What happens when you call `add()` on an `Iterator` obtained from an `ArrayList`?
- Which `Collection` interface provides a method to efficiently add elements at both ends?
- What is the Big O notation for checking if an element exists in a `TreeSet`?
- Which `Map` implementation is guaranteed to provide its entries in ascending order of keys?
- What is the result of calling `collections.reverseOrder()`?
More quizzes
- JVM Internals▶ Play
- Memory Management▶ Play
- Syntax▶ Play
- Threads and Concurrency▶ Play
- Web Applications▶ Play