This set of Java Problems on “Java.util – Dictionary, Hashtable & Properties”.
1. Which of these class object uses the key to store value? Answer: d 2. Which of these method is used to insert value and its key? Answer: a 3. Which of these is the interface of legacy is implemented by Hashtable and Dictionary classes? Answer: a 4. Which of these is a class which uses String as a key to store the value in object? Answer: d 5. Which of these methods is used to retrieve the elements in properties object at specific location? Answer: d 6. What will be the output of the following Java code? a) 0 7. What will be the output of the following Java code? a) 0 8. What will be the output of the following Java code? a) {C=8, B=2} 9. What will be the output of the following Java code? a) {C=8, B=2} 10. What will be the output of the following Java code? a) {AB, BC, CD}
a) Dictionary
b) Map
c) Hashtable
d) All of the mentioned
Clarification: Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.
a) put()
b) set()
c) insertElement()
d) addElement()
Clarification: None.
a) Map
b) Enumeration
c) HashMap
d) Hashtable
Clarification: Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.
a) Array
b) ArrayList
c) Dictionary
d) Properties
Clarification: None.
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()
Clarification: None.
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)));
}
}
b) 1
c) true
d) false
Clarification: 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
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());
}
}
b) 1
c) 2
d) 3
Clarification: None.
Output:
$ javac hashtable.java
$ java hashtable
0
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);
}
}
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
Clarification: None.
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}
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());
}
}
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
Clarification: 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}
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());
}
}
b) [AB, BC, CD]
c) [3, 2, 8]
d) {3, 2, 8}
Clarification: 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].