Here are 10 essential multiple-choice questions on Java Hashtable, covering key concepts.
Question 1
Which of the following statements about Hashtable is true?
Hashtable allows one null key but multiple null values
Hashtable allows duplicate keys
Hashtable does not allow null keys or null values
Hashtable maintains insertion order
Question 2
What happens when you attempt to insert a null key in a Hashtable?
The key-value pair is inserted normally
A NullPointerException is thrown
The key is ignored, but the value is stored
A RuntimeException is thrown
Question 3
What is the default capacity of Hashtable when using the no-argument constructor?
8
11
16
32
Question 4
What is the time complexity of get() and put() operations in Hashtable in the best-case scenario?
O(1)
O(log n)
O(n)
O(n log n)
Question 5
How does Hashtable handle concurrency?
It uses explicit locks for synchronization
All methods are synchronized, making it thread-safe
It is non-thread-safe and requires external synchronization
It uses CopyOnWriteArrayList internally for thread safety
Question 6
What will be the output of the following code?
import java.util.*;
public class Test {
public static void main(String[] args) {
Hashtable<Integer, String> table = new Hashtable<>();
table.put(1, "One");
table.put(2, "Two");
table.put(3, "Three");
table.remove(2);
System.out.println(table);
}
}
{1=One, 2=Two, 3=Three}
{1=One, 3=Three}
{1=One, 2=Two}
Compilation Error
Question 7
What happens if two keys have the same hash code in a Hashtable?
One of the keys is removed
The new key replaces the old key
Hashtable uses chaining (linked list) to store both keys
Hashtable throws an exception
Question 8
What will be the output of the following code?
import java.util.*;
public class Test {
public static void main(String[] args) {
Map<Integer, String> table = new Hashtable<>();
table.put(1, "A");
table.put(2, "B");
table.put(1, "C");
System.out.println(table.get(1));
}
}
A
B
C
null
Question 9
What is the difference between Hashtable and ConcurrentHashMap?
ConcurrentHashMap allows null keys, while Hashtable does not
ConcurrentHashMap synchronizes at method level, while Hashtable synchronizes at block level
ConcurrentHashMap uses a segmented locking mechanism, making it more efficient
Hashtable is faster than ConcurrentHashMap
Question 10
What will happen when iterating over a Hashtable using an iterator and a modification is made concurrently?
The iterator continues normally
The iterator throws a ConcurrentModificationException
The modification is ignored
The program enters an infinite loop
There are 10 questions to complete.