Java Multiple Choice Questions & Answers on Java.util package Maps for Freshers

1. Which of these object stores association between keys and values?

a) Hash table
b) Map
c) Array
d) String
Answer: b
2. Which of these classes provide implementation of map interface?

a) ArrayList
b) HashMap
c) LinkedList
d) DynamicList
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()
Answer: b
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()
Answer: b
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()
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.
6. Which of these method is used add an element and corresponding key to a map?

a) put()
b) set()
c) redo()
d) add()
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 is the output of this program?
  1.     import java.util.*;
  2.     class Maps {
  3.         public static void main(String args[]) {
  4.             HashMap obj = new HashMap();
  5.             obj.put("A", new Integer(1));
  6.             obj.put("B", new Integer(2));
  7.             obj.put("C", new Integer(3));
  8.             System.out.println(obj);
  9.         }
  10.     }
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}
Answer: d
8. What is the output of this program?
  1.     import java.util.*;
  2.     class Maps {
  3.         public static void main(String args[]) {
  4.             HashMap obj = new HashMap();
  5.             obj.put("A", new Integer(1));
  6.             obj.put("B", new Integer(2));
  7.             obj.put("C", new Integer(3));
  8.             System.out.println(obj.keySet());
  9.         }
  10.     }
a) [A, B, C] 
b) {A, B, C}
c) {1, 2, 3}
d) [1, 2, 3] 

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.
9. What is the output of this program?
  1.     import java.util.*;
  2.     class Maps {
  3.         public static void main(String args[]) {
  4.             HashMap obj = new HashMap();
  5.             obj.put("A", new Integer(1));
  6.             obj.put("B", new Integer(2));
  7.             obj.put("C", new Integer(3));
  8.             System.out.println(obj.get("B"));
  9.         }
  10.     }
a) 1
b) 2
c) 3
d) null
Answer: 2

Explanation: obj.get(“B”) method is used to obtain the value associated with key “B”, which is 2.
10. What is the output of this program?
  1.     import java.util.*;
  2.     class Maps {
  3.         public static void main(String args[]) {
  4.             TreeMap obj = new TreeMap();
  5.             obj.put("A", new Integer(1));
  6.             obj.put("B", new Integer(2));
  7.             obj.put("C", new Integer(3));
  8.             System.out.println(obj.entrySet());
  9.         }
  10.     }
a) [A, B, C] 
b) [1, 2, 3] 
c) {A=1, B=2, C=3}

d) [A=1, B=2, C=3]

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.

Related

CSS Multiple Choice Questions & Answers on Specificity and Importance for Freshers

Find the specificity of the statments using the rule given below.A selector’s specificity is calculated as follows:1)count 1 if the declaration is from is a ‘style’ attribute rather than a rule with...

C Programming Questions and Answers on Pointers and Function Arguments for Freshers

1. What is the output of this C code? #include void foo(int*); int main() { int i = 10; foo((&i)++); } void foo(int *p) { ...

Java Multiple Choice Questions & Answers on Java.lang – Integer, Long & Character Wrappers for Freshers

1. Which of these is a wrapper for data type int? a) Integerb) Longc) Byted) Double Answer: a 2. Which of the following methods is a method of wrapper Integer for obtaining hash code for the invo...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item