Java LinkedHashSet MCQ Questions and Answers

1. What is a LinkedHashSet in Java?

a) A type of list
b) A type of map
c) A hashed linked list
d) A set implementation that maintains a linked list across its elements

Answer:

d) A set implementation that maintains a linked list across its elements

Explanation:

LinkedHashSet is a HashSet with a linked list running through its entries to maintain order.

2. How does LinkedHashSet maintain insertion order?

a) By sorting the elements
b) By using a hash table
c) By using a linked list
d) By using an array

Answer:

c) By using a linked list

Explanation:

LinkedHashSet maintains insertion order through a doubly-linked list across its elements.

3. What interfaces does LinkedHashSet implement?

a) Set and List
b) Set and Map
c) Set and SortedSet
d) Set only

Answer:

d) Set only

Explanation:

LinkedHashSet implements the Set interface.

4. Can a LinkedHashSet contain duplicate elements?

a) Yes
b) No
c) Only null values can be duplicated
d) Only if elements are strings

Answer:

b) No

Explanation:

LinkedHashSet does not allow duplicate elements, similar to HashSet.

5. Which method do you use to add elements in a LinkedHashSet?

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

Answer:

b) add()

Explanation:

Elements are added to a LinkedHashSet using the add() method.

6. Does LinkedHashSet allow null elements?

a) Yes
b) No
c) Only one null element
d) Only if it's the first element

Answer:

a) Yes

Explanation:

LinkedHashSet allows null elements.

7. How do you remove an element from a LinkedHashSet?

a) removeElement()
b) delete()
c) remove()
d) discard()

Answer:

c) remove()

Explanation:

The remove() method is used to remove an element from a LinkedHashSet.

8. What is the initial default capacity of a LinkedHashSet?

a) 10
b) 16
c) 32
d) 8

Answer:

b) 16

Explanation:

The default initial capacity of a LinkedHashSet is 16.

9. What is the time complexity of basic operations like add, remove, and contains in a LinkedHashSet?

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

Answer:

a) O(1)

Explanation:

The basic operations in LinkedHashSet typically have a constant time complexity.

10. What advantage does LinkedHashSet have over HashSet?

a) Faster performance
b) Maintains insertion order
c) Allows duplicates
d) Smaller memory footprint

Answer:

b) Maintains insertion order

Explanation:

The main advantage of LinkedHashSet over HashSet is that it maintains the order of elements as they were inserted.

11. What method is used to get the number of elements in a LinkedHashSet?

a) length()
b) size()
c) count()
d) getSize()

Answer:

b) size()

Explanation:

The size() method returns the number of elements in the LinkedHashSet.

12. Can LinkedHashSet be synchronized?

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

Answer:

a) Yes

Explanation:

LinkedHashSet can be synchronized by using Collections.synchronizedSet().

13. What happens when you iterate over a LinkedHashSet?

a) The elements are returned in random order
b) The elements are returned in sorted order
c) The elements are returned in the order of insertion
d) The elements are returned in reverse order

Answer:

c) The elements are returned in the order of insertion

Explanation:

When iterating over a LinkedHashSet, the elements are returned in the order they were inserted.

14. How do you clear all elements from a LinkedHashSet?

a) clear()
b) removeAll()
c) reset()
d) emptySet()

Answer:

a) clear()

Explanation:

The clear() method removes all elements from the LinkedHashSet.

15. What happens if you add a duplicate element to a LinkedHashSet?

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 LinkedHashSet, it is not added again, maintaining the uniqueness of elements.


Comments