Java Multiple Choice Questions & Answers on Java.util package Dictionary, Hashtable & Properties for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_24.html
1. Which of these class object uses key to store value?
a) Dictionary
b) Map
c) Hashtable
d) All if the mentioned
Answer: d
Explanation: Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.
2. Which of these method is used to insert value and its key?
a) put()
b) set()
c) insertElement()
d) addElement()
Answer: a
3. Which of these is the interface of legacy is implemented by Hashtable and Dictionary classes?
a) Map
b) Enumeration
c) HashMap
d) Hashtable
Answer: a
Explanation: Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.
4. Which of these is a class which uses String as a key to store the value in object?
a) Array
b) ArrayList
c) Dictionary
d) Properties
Answer: d
5. Which of these methods is used to retrieve the elements in properties object at specific location?
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()
Answer: d
6. What is the output of this program?
import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
System.out.print(obj.contains(new Integer(5)));
}
}
a) 0
b) 1
c) true
d) false
b) 1
c) true
d) false
Answer: d
Explanation: Hashtable object obj contains values 3, 2, 8 when obj.contains(new Integer(5)) is executed it searches for 5 in the hashtable since it is not present false is returned.
7. What is the output of this program?
import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
obj.clear();
System.out.print(obj.size());
}
}
a) 0
b) 1
c) 2
d) 3
b) 1
c) 2
d) 3
Answer: a
8. What is the output of this program?
import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
obj.remove(new String("A"));
System.out.print(obj);
}
}
a) {C=8, B=2}
b) [C=8, B=2]
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
d) [A=3, C=8, B=2]
Answer: a
9. What is the output of this program?
import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
System.out.print(obj.toString());
}
}
a) {C=8, B=2}
b) [C=8, B=2]
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
d) [A=3, C=8, B=2]
Answer: c
Explanation: obj.toString returns String equivalent of the hashtable, which can also be obtained by simply writing System.out.print(obj); as print system automatically coverts the obj to string equivalent.
10. What is the output of this program?
import java.util.*;
class properties {
public static void main(String args[]) {
Properties obj = new Properties();
obj.put("AB", new Integer(3));
obj.put("BC", new Integer(2));
obj.put("CD", new Integer(8));
System.out.print(obj.keySet());
}
}
a) {AB, BC, CD}
b) [AB, BC, CD]
b) [AB, BC, CD]
c) [3, 2, 8]
d) {3, 2, 8}
Answer: B
Explanation: obj.keySet() returns a set containing all the keys used in properties object, here obj contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD].