What will be the output of the following Java program?

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

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

1 thought on “What will be the output of the following Java program?”

  1. 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

Leave a Comment