This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-Set”.
1. What is the default clone of HashSet? Answer: b 2. Do we have get(Object o) method in HashSet. Answer: b 3. What does Collections.emptySet() return? Answer: a 4. What are the initial capacity and load factor of HashSet? Answer: c 5. What is the relation between hashset and hashmap? Answer: a 6. What will be the output of the following Java code snippet? a) b) Test – 10 Answer: a 7. Set has contains(Object o) method. Answer: a 8. What is the difference between TreeSet and SortedSet? Answer: d 9. What happens if two threads simultaneously modify TreeSet? Answer: a 10. What is the unique feature of LinkedHashSet? Answer: b
a) Deep clone
b) Shallow clone
c) Plain clone
d) Hollow clone
Clarification: Default clone() method uses shallow copy. The internal elements are not cloned. A shallow copy only copies the reference object.
a) True
b) False
Clarification: get(Object o) method is useful when we want to compare objects based on the comparison of values. HashSet does not provide any way to compare objects. It just guarantees unique objects stored in the collection.
a) Immutable Set
b) Mutable Set
c) The type of Set depends on the parameter passed to the emptySet() method
d) Null object
Clarification: 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.
a) 10, 1.0
b) 32, 0.75
c) 16, 0.75
d) 32, 1.0
Clarification: We should not set the initial capacity too high and load factor too low if iteration performance is needed.
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
Clarification: 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.
public class Test
{
public static void main(String[] args)
{
Set s = new HashSet();
s.add(new Long(10));
s.add(new Integer(10));
for(Object object : s)
{
System.out.println("test - "+object);
}
}
}
Test - 10
Test - 10
c) Runtime Exception
d) Compilation Failure
Clarification: Integer and Long are two different data types and different objects. So they will be treated as unique elements and not overridden.
a) True
b) False
Clarification: Set has contains(Object o) method instead of get(Object o) method as get is needed for comparing object and getting corresponding value.
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
Clarification: SortedSet is an interface. It maintains an ordered set of elements. TreeSet is an implementation of SortedSet.
a) ConcurrentModificationException is thrown
b) Both threads can perform action successfully
c) FailFastException is thrown
d) IteratorModificationException is thrown
Clarification: TreeSet provides fail-fast iterator. Hence when concurrently modifying TreeSet it throws ConcurrentModificationException.
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
Clarification: 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.