250+ TOP MCQs on Java.util – Dictionary, Hashtable & Properties and Answers

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

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

Answer: a
Clarification: 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
Clarification: 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()

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

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

7. 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.clear();
  11.             System.out.print(obj.size());
  12.         }
  13.     }

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

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

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

Answer: c
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}

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}

Answer: b
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].

Leave a Reply

Your email address will not be published. Required fields are marked *