Java Questions & Answers – Java.util – Dictionary, Hashtable & Properties

This set of Java Problems 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

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()
View Answer

Answer: a
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

Answer: a
Explanation: Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object.
advertisement
advertisement

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

Answer: d
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

Answer: d
Explanation: None.

6. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class hashtable 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Hashtable obj = new Hashtable();
  7.             obj.put("A", new Integer(3));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(8));
  10.             System.out.print(obj.contains(new Integer(5)));
  11.         }
  12.     }

a) 0
b) 1
c) true
d) false
View Answer

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.
Output:

advertisement
$ javac hashtable.java
$ java hashtable
false

7. What will be the output of the following Java code?

advertisement
  1.     import java.util.*;
  2.     class hashtable 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Hashtable obj = new Hashtable();
  7.             obj.put("A", new Integer(3));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(8));
  10.             obj.clear();
  11.             System.out.print(obj.size());
  12.         }
  13.     }

a) 0
b) 1
c) 2
d) 3
View Answer

Answer: a
Explanation: None.
Output:

$ javac hashtable.java
$ java hashtable
0

8. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class hashtable 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Hashtable obj = new Hashtable();
  7.             obj.put("A", new Integer(3));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(8));
  10.             obj.remove(new String("A"));
  11.             System.out.print(obj);
  12.         }
  13.     }

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

Answer: a
Explanation: None.
Output:

$ javac hashtable.java
$ java hashtable
{C=8, B=2}

9. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class hashtable 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Hashtable obj = new Hashtable();
  7.             obj.put("A", new Integer(3));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(8));
  10.             System.out.print(obj.toString());
  11.         }
  12.     }

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

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 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?

  1.     import java.util.*;
  2.     class properties 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Properties obj = new Properties();
  7.             obj.put("AB", new Integer(3));
  8.             obj.put("BC", new Integer(2));
  9.             obj.put("CD", new Integer(8));
  10.             System.out.print(obj.keySet());
  11.         }
  12.     }

a) {AB, BC, CD}
b) [AB, BC, CD]
c) [3, 2, 8]
d) {3, 2, 8}
View Answer

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].
Output:

$ javac properties.java
$ java properties
[AB, BC, CD].

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.