250+ TOP MCQs on Data Structures-Set and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-Set”.

1. What is the default clone of HashSet?
a) Deep clone
b) Shallow clone
c) Plain clone
d) Hollow clone

Answer: b
Clarification: 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.
a) True
b) False

Answer: b
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.

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

Answer: a
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.

4. What are the initial capacity and load factor of HashSet?
a) 10, 1.0
b) 32, 0.75
c) 16, 0.75
d) 32, 1.0

Answer: c
Clarification: 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

Answer: a
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.

6. What will be the output of the following Java code snippet?

  1. public class Test 
  2. {
  3. 	public static void main(String[] args) 
  4.         {
  5. 		Set s = new HashSet();
  6. 		s.add(new Long(10));
  7. 		s.add(new Integer(10));
  8. 		for(Object object : s)
  9.                 {
  10. 		    System.out.println("test - "+object);
  11. 		}
  12. 	}
  13. }

a)

   Test - 10
   Test - 10

b) Test – 10
c) Runtime Exception
d) Compilation Failure

Answer: a
Clarification: 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.
a) True
b) False

Answer: a
Clarification: 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

Answer: d
Clarification: 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

Answer: a
Clarification: 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

Answer: b
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.

Leave a Reply

Your email address will not be published. Required fields are marked *