Which interface in Java represents a collection that does not allow duplicate elements?
A
List B
Map C
Set D
Queue
Analysis & Theory
`Set` is a collection that does not allow duplicate elements.
Which of the following classes implements the Set interface?
A
ArrayList B
HashSet C
TreeMap D
LinkedList
Analysis & Theory
`HashSet` is a common implementation of the `Set` interface.
What is the time complexity for adding an element to a HashSet (on average)?
A
O(1) B
O(log n) C
O(n) D
O(n log n)
Analysis & Theory
On average, adding an element to a `HashSet` takes O(1) time.
Which Set implementation maintains the insertion order?
A
TreeSet B
HashSet C
LinkedHashSet D
None of the above
Analysis & Theory
`LinkedHashSet` maintains the order in which elements were inserted.
Which Set implementation stores elements in a sorted (natural) order?
A
HashSet B
TreeSet C
LinkedHashSet D
ArrayList
Analysis & Theory
`TreeSet` stores elements in their natural sorted order.
Can a Set contain null elements?
A
No B
Yes, always C
Only HashSet allows one null D
Only TreeSet allows one null
Analysis & Theory
`HashSet` allows one null element, but `TreeSet` throws `NullPointerException` when sorting nulls.
What happens if you try to add a duplicate element to a Set?
A
It throws an error B
It replaces the old value C
It adds the duplicate D
It is ignored silently
Analysis & Theory
Adding a duplicate to a `Set` is ignored; it does not raise an error or add it.
Which method is used to check if a Set contains a particular element?
A
get() B
contains() C
has() D
exists()
Analysis & Theory
`contains(Object o)` checks if the Set has a specific element.
What is the output of `set.size()` if the set contains [1, 2, 2, 3]?
A
4 B
3 C
2 D
1
Analysis & Theory
Sets do not allow duplicates, so the size would be `3`.
Which of the following is true about TreeSet?
A
It allows duplicate values B
It maintains insertion order C
It is not sorted D
It does not allow null elements
Analysis & Theory
`TreeSet` does not allow null elements and stores items in sorted order.