This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Data Structures-Set”.
1. What is the default clone of HashSet?
a) Deep clone
b) Shallow clone
c) Plain clone
d) Hollow clone
Explanation: Default clone() method uses shallow copy. The internal elements are not cloned. A shallow copy only copies the reference object.
2. Do we have get(Object o) method in HashSet?
Explanation: get(Object o) method is useful when we want to compare objects based on comparison of values. HashSet does not provide any way to compare objects. It just guarantees unique objects stored in the collection.
3. What does Collections.emptySet() return?
a) Immutable Set
b) Mutable Set
c) The type of Set depends on the parameter passed to the emptySet() method
d) Null object
Explanation: Immutable Set is useful in multithreaded environment. One does not need to declare generic type collection. It is inferred by the context of method call.
4. What is the initial capacity and load factor of HashSet?
a) 10, 1.0
b) 32, 0.75
c) 16, 0.75
d) 32, 1.0
Explanation: We should not set the initial capacity too high and load factor too low if iteration performance is needed.
5. What is the relation between hashset and hashmap?
a) HashSet internally implements HashMap
b) HashMap internally implements HashSet
c) HashMap is the interface; HashSet is the concrete class
d) HashSet is the interface; HashMap is the concrete class
Explanation: HashSet is implemented to provide uniqueness feature which is not provided by HashMap. This also reduces code duplication and provides the memory efficient behavior of HashMap.
6. What is the output of below code snippet?
public class Test
public static void main(String args)
Set s = new HashSet();
for(Object object : s)
System.out.println("test - "+object);
a) Test – 10
Test – 10
b) Test – 10
c) Runtime Exception
d) Compilation Failure
Explanation: Integer and Long are two different data types and different objects. So they will be treated as unique elements and not overridden.
7. Set has contains(Object o) method?
Explanation: Set has contains(Object o) method instead of get(Object o) method as get is needed for comparing object and getting corresponding value.
8. What is the difference between TreeSet and SortedSet?
a) TreeSet is more efficient than SortedSet
b) SortedSet is more efficient than TreeSet
c) TreeSet is an interface; SortedSet is a concrete class
d) SortedSet is an interface; TreeSet is a concrete class
Explanation: SortedSet is an interface. It maintains an ordered set of elements. TreeSet is an implementation of SortedSet.
9. What happens if two threads simultaneously modify TreeSet?
a) ConcurrentModificationException is thrown
b) Both threads can perform action successfully
c) FailFastException is thrown
d) IteratorModificationException is thrown
Explanation: TreeSet provides fail-fast iterator. Hence when concurrently modifying TreeSet it throws ConcurrentModificationException.
10. What is the unique feature of LinkedHashSet?
a) It is not a valid class
b) It maintains the insertion order and guarantees uniqueness
c) It provides a way to store key values with uniqueness
d) The elements in the collection are linked to each other
Explanation: Set is a collection of unique elements.HashSet has the behavior of Set and stores key value pairs. The LinkedHashSet stores the key value pairs in the order of insertion.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all features of Java programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.