This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java.util – Dictionary, Hashtable & Properties”.
1. Which of these class object uses the key to store value?
a) Dictionary
b) Map
c) Hashtable
d) All of the mentioned
View Answer
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()
View Answer
Explanation: None.
3. Which of these is the interface of legacy is implemented by Hashtable and Dictionary classes?
a) Map
b) Enumeration
c) HashMap
d) Hashtable
View Answer
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
View Answer
Explanation: None.
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()
View Answer
Explanation: None.
6. What will be the output of the following Java code?
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
View Answer
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.
Output:
$ javac hashtable.java $ java hashtable false
7. What will be the output of the following Java code?
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
View Answer
Explanation: None.
Output:
$ javac hashtable.java $ java hashtable 0
8. What will be the output of the following Java code?
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]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
View Answer
Explanation: None.
Output:
$ javac hashtable.java $ java hashtable {C=8, B=2}
9. What will be the output of the following Java code?
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]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
View Answer
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 converts the obj tostring equivalent.
Output:
$ javac hashtable.java $ java hashtable {A=3, C=8, B=2}
10. What will be the output of the following Java code?
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]
c) [3, 2, 8]
d) {3, 2, 8}
View Answer
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].
Output:
$ javac properties.java $ java properties [AB, BC, CD].
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Java Internship
- Check Programming Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Java Books