Java HashSet MCQ Questions and Answers

1. What is a HashSet in Java?

a) A dynamic array
b) A collection that uses a hash table for storage
c) A linked list
d) A data structure for key-value pairs

Answer:

b) A collection that uses a hash table for storage

Explanation:

HashSet in Java is a collection that uses a hash table, providing efficient storage and retrieval.

2. What is the primary characteristic of a HashSet?

a) Ordered elements
b) Duplicate elements
c) Sorted elements
d) Unique elements

Answer:

d) Unique elements

Explanation:

HashSet stores unique elements only; it does not allow duplicates.

3. Which interface does HashSet implement?

a) List
b) Map
c) Set
d) Queue

Answer:

c) Set

Explanation:

HashSet implements the Set interface.

4. How does HashSet store its elements?

a) In index-based order
b) In a sequential list
c) Based on hash codes of the elements
d) In sorted order

Answer:

c) Based on hash codes of the elements

Explanation:

HashSet stores elements by using a hash table, where elements are placed according to their hash codes.

5. Can HashSet contain null values?

a) Yes
b) No
c) Only one null value
d) Only if it's empty

Answer:

c) Only one null value

Explanation:

HashSet can contain one null value.

6. What is the time complexity of basic operations such as add, remove, and contains in HashSet?

a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

Answer:

a) O(1)

Explanation:

Basic operations like add, remove, and contains have a constant time complexity O(1) in a HashSet.

7. How do you add elements to a HashSet?

a) add()
b) put()
c) insert()
d) append()

Answer:

a) add()

Explanation:

The add() method is used to add elements to a HashSet.

8. How do you check if a HashSet contains a specific element?

a) find()
b) search()
c) contains()
d) hasElement()

Answer:

c) contains()

Explanation:

The contains() method is used to check if an element exists in a HashSet.

9. What happens if you try to add a duplicate element to a HashSet?

a) The element is added at the end
b) The element replaces the existing one
c) The duplicate is ignored
d) An exception is thrown

Answer:

c) The duplicate is ignored

Explanation:

If a duplicate element is added to a HashSet, it is simply ignored.

10. How do you remove all elements from a HashSet?

a) clear()
b) removeAll()
c) deleteAll()
d) empty()

Answer:

a) clear()

Explanation:

The clear() method is used to remove all elements from a HashSet, making it empty.

11. What happens when you iterate over a HashSet?

a) The elements are returned in sorted order
b) The elements are returned in the order they were added
c) The order of elements is unpredictable
d) Elements are returned in reverse order

Answer:

c) The order of elements is unpredictable

Explanation:

Iterating over a HashSet does not guarantee any specific order of elements.

12. Can a HashSet be synchronized?

a) Yes
b) No
c) Only in multi-threaded applications
d) Only if it's empty

Answer:

a) Yes

Explanation:

A HashSet can be synchronized by wrapping it with Collections.synchronizedSet().

13. Is it possible to convert a HashSet to an ArrayList?

a) Yes
b) No
c) Only if it has less than 10 elements
d) Only if it's sorted

Answer:

a) Yes

Explanation:

A HashSet can be converted to an ArrayList by passing the set to the ArrayList constructor.

14. What constructor can be used to create a HashSet with a specified initial capacity?

a) HashSet(int capacity)
b) HashSet()
c) HashSet(Collection c)
d) HashSet(int capacity, float loadFactor)

Answer:

a) HashSet(int capacity)

Explanation:

The HashSet(int capacity) constructor creates a HashSet with the specified initial capacity.

15. How can you ensure the elements of a HashSet are sorted?

a) By using the sort() method
b) HashSet elements cannot be sorted
c) By converting it to a TreeSet
d) By using Collections.sort()

Answer:

c) By converting it to a TreeSet

Explanation:

Elements in a HashSet cannot be sorted directly; however, you can convert the HashSet to a TreeSet to sort the elements.

Leave a Comment

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

Scroll to Top