Hashtable MCQs
What is a primary characteristic of a Hashtable in Java?
A. It allows null keys and values.
B. It is synchronized.
C. It maintains the order of elements.
D. It is unsynchronized.
Which method is used to add a key-value pair to a Hashtable?
A. add(key, value)
B. insert(key, value)
C. put(key, value)
D. set(key, value)
What will happen if you try to insert a null key or value into a Hashtable?
A. It will store the null key or value.
B. It will throw a NullPointerException.
C. It will store the value as an empty string.
D. It will store the key as "null".
How do you check if a specific key exists in a Hashtable?
A. hasKey(key)
B. keyExists(key)
C. containsKey(key)
D. findKey(key)
Which method removes a key-value pair from a Hashtable?
A. remove(key)
B. delete(key)
C. discard(key)
D. erase(key)
Which of the following best describes the Hashtable class in Java?
A. Allows duplicate keys
B. Allows null values
C. Synchronized
D. Maintains insertion order
What will table.get("key") return if "key" is not present in the Hashtable?
A. null
B. Throws NullPointerException
C. 0
D. An empty string
What method is used to check if a specific value exists in a Hashtable?
A. contains(value)
B. hasValue(value)
C. valueExists(value)
D. containsValue(value)
Which method provides a set view of the keys contained in a Hashtable?
A. keySet()
B. keys()
C. getKeys()
D. allKeys()
How can you clear all entries in a Hashtable?
A. clear()
B. deleteAll()
C. removeAll()
D. eraseAll()
Which method returns the number of key-value pairs in a Hashtable?
A. size()
B. count()
C. length()
D. numberOfEntries()
What happens if two different keys have the same hash code in a Hashtable?
A. Only one key-value pair is stored.
B. The second key-value pair replaces the first one.
C. Both key-value pairs are stored in a linked list at the hash index.
D. An exception is thrown.
What is the time complexity for inserting an element in a Hashtable?
A. O(1)
B. O(n)
C. O(log n)
D. O(n^2)
What will be the output of the following code?
java
Copy code
Hashtable<String, Integer> table = new Hashtable<>();
table.put("A", 1);
table.put("B", 2);
table.put("A", 3);
System.out.println(table.get("A"));
A. 1
B. 2
C. 3
D. null
What is a key difference between Hashtable and HashMap?
A. Hashtable allows null keys and values, whereas HashMap does not.
B. Hashtable is synchronized, while HashMap is not.
C. Hashtable maintains insertion order, whereas HashMap does not.
D. Hashtable is part of the java.util.concurrent package, while HashMap is not.
Which method is used to create a shallow copy of a Hashtable?
A. clone()
B. copy()
C. duplicate()
D. newInstance()
What is the default initial capacity of a Hashtable in Java?
A. 8
B. 11
C. 16
D. 20
What will table.isEmpty() return if the Hashtable contains no key-value pairs?
A. true
B. false
C. null
D. 0
Which method is used to retrieve all values in a Hashtable?
A. values()
B. allValues()
C. getValues()
D. valueSet()
What will be the result of table.containsKey("X") if the key "X" is not present in the Hashtable?
A. true
B. false
C. null
D. Throws an exception
Answers:
B. It is synchronized.
C. put(key, value)
B. It will throw a NullPointerException.
C. containsKey(key)
A. remove(key)
C. Synchronized
A. null
D. containsValue(value)
A. keySet()
A. clear()
A. size()
C. Both key-value pairs are stored in a linked list at the hash index.
A. O(1)
C. 3
B. Hashtable is synchronized, while HashMap is not.
A. clone()
B. 11
A. true
A. values()
B. false
No comments:
Post a Comment