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
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
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
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
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
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?
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);
}
}
}
a)
Test - 10 Test - 10
b) Test – 10
c) Runtime Exception
d) Compilation Failure
View Answer
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
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
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
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
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.
- Check Java Books
- Practice Information Technology MCQs
- Practice BCA MCQs
- Practice Programming MCQs
- Check Programming Books