Java Questions & Answers – Data Structures-Set

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
View Answer

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

Answer: b
Explanation: 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
View Answer

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

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

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


   Test - 10
   Test - 10

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

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

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

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

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

Answer: b
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 areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.