HashSet MCQs
What is the primary characteristic of a HashSet in Java?
A. It allows duplicate elements.
B. It maintains insertion order.
C. It does not allow duplicate elements.
D. It is synchronized.
Which interface does HashSet implement?
A. List
B. Set
C. Queue
D. Map
What will be the output of the following code?
java
Copy code
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("A");
System.out.println(set);
A. [A, B, A]
B. [A, B]
C. [B, A]
D. null
Which method is used to remove an element from a HashSet?
A. remove(element)
B. delete(element)
C. discard(element)
D. erase(element)
What is the time complexity of adding an element to a HashSet?
A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)
How do you check if a HashSet contains a specific element?
A. contains(element)
B. has(element)
C. find(element)
D. includes(element)
What is the default initial capacity of a HashSet in Java?
A. 10
B. 16
C. 20
D. 32
Which method is used to get the size of a HashSet?
A. size()
B. length()
C. count()
D. getSize()
Which of the following operations is NOT supported by HashSet?
A. Adding elements
B. Removing elements
C. Retrieving elements by index
D. Checking if an element exists
What will be the output of the following code?
java
Copy code
HashSet<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
set.remove(2);
System.out.println(set);
A. [1, 2, 3]
B. [1, 3]
C. [2, 1, 3]
D. [1]
Which method is used to clear all elements from a HashSet?
A. clear()
B. deleteAll()
C. removeAll()
D. empty()
Which of the following statements is true about HashSet?
A. HashSet is synchronized.
B. HashSet maintains the order of elements.
C. HashSet does not guarantee any specific order of elements.
D. HashSet allows duplicate elements.
What will be the result of calling hashSet.isEmpty() on an empty HashSet?
A. true
B. false
C. null
D. 0
What does the HashSet method addAll(Collection c) do?
A. Adds all elements from the specified collection to the HashSet.
B. Removes all elements from the specified collection from the HashSet.
C. Replaces all elements in the HashSet with elements from the specified collection.
D. Checks if all elements in the specified collection are present in the HashSet.
Which method in HashSet allows you to iterate over its elements?
A. iterator()
B. forEach()
C. stream()
D. traverse()
What will hashSet.size() return for a HashSet with three elements?
A. 3
B. 2
C. 1
D. null
Which method is used to check if a HashSet is a subset of another collection?
A. containsAll(Collection c)
B. isSubsetOf(Collection c)
C. checkSubset(Collection c)
D. subsetOf(Collection c)
What will be the result of hashSet.contains(null) if null is not present in the HashSet?
A. false
B. true
C. null
D. 0
Which constructor parameter can be used to specify the initial capacity of a HashSet?
A. new HashSet(int initialCapacity)
B. new HashSet(int capacity)
C. new HashSet(int size)
D. new HashSet(int maxCapacity)
Which method in HashSet can be used to check for equality between two HashSet instances?
A. equals(Object o)
B. isEqual(Object o)
C. compareTo(Object o)
D. matches(Object o)
Answers:
C. It does not allow duplicate elements.
B. Set
B. [A, B]
A. remove(element)
A. O(1)
A. contains(element)
B. 16
A. size()
C. Retrieving elements by index
B. [1, 3]
A. clear()
C. HashSet does not guarantee any specific order of elements.
A. true
A. Adds all elements from the specified collection to the HashSet.
A. iterator()
A. 3
A. containsAll(Collection c)
A. false
A. new HashSet(int initialCapacity)
A. equals(Object o)
No comments:
Post a Comment