Java Questions & Answers – Java.util – Maps

This section of our 1000+ Java MCQs focuses on Maps of Java Programming Language.

1. Which of these object stores association between keys and values?
a) Hash table
b) Map
c) Array
d) String
View Answer

Answer: b
Explanation: None.

2. Which of these classes provide implementation of map interface?
a) ArrayList
b) HashMap
c) LinkedList
d) DynamicList
View Answer

Answer: b
Explanation: AbstractMap, WeakHashMap, HashMap and TreeMap provide implementation of map interface.

3. Which of these method is used to remove all keys/values pair from the invoking map?
a) delete()
b) remove()
c) clear()
d) removeAll()
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

4. Which of these method Map class is used to obtain an element in the map having specified key?
a) search()
b) get()
c) set()
d) look()
View Answer

Answer: b
Explanation: None.

5. Which of these methods can be used to obtain set of all keys in a map?
a) getAll()
b) getKeys()
c) keyall()
d) keySet()
View Answer

Answer: d
Explanation: keySet() methods is used to get a set containing all the keys used in a map. This method provides set view of the keys in the invoking map.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of these method is used add an element and corresponding key to a map?
a) put()
b) set()
c) redo()
d) add()
View Answer

Answer: a
Explanation: Maps revolve around two basic operations – get() and put(). to put a value into a map, use put(), specifying the key and the value. To obtain a value, call get() , passing the key as an argument. The value is returned.

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

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

a) {A 1, B 1, C 1}
b) {A, B, C}
c) {A-1, B-1, C-1}
d) {A=1, B=2, C=3}
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac Maps.java
$ java Maps 
{A=1, B=2, C=3}

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

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

a) [A, B, C]
b) {A, B, C}
c) {1, 2, 3}
d) [1, 2, 3]
View Answer

Answer: a
Explanation: keySet() method returns a set containing all the keys used in the invoking map. Here keys are characters A, B & C. 1, 2, 3 are the values given to these keys.
Output:

$ javac Maps.java
$ java Maps 
[A, B, C].

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

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

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

Answer: b
Explanation: obj.get(“B”) method is used to obtain the value associated with key “B”, which is 2.
Output:

$ javac Maps.java
$ java Maps 
2

10. What will be the output of the following Java program?

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

a) [A, B, C]
b) [1, 2, 3]
c) {A=1, B=2, C=3}
d) [A=1, B=2, C=3]
View Answer

Answer: d
Explanation: obj.entrySet() method is used to obtain a set that contains the entries in the map. This method provides set view of the invoking map.
Output:

$ javac Maps.java
$ java Maps 
[A=1, B=2, C=3].

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.