Menu
Topics Index
...
`

Map Example


If you need explanation Read this topic

If you need Answer Take test on this topic

Map Example
What will be the output of the following program?
import java.util.*;
public class MapExample {
    public static void main(String args[]) {
        Map map = new HashMap();
        map.put("key1", "family");
        map.put("key2", "relatives");
        map.put("key3", "friends");
        System.out.println(map);
        String str = (String) map.get("key1");
        System.out.print(str);
    }
}

A. {key3=friends, key2=relatives, key1=family}
family
B. {key1=family, key2=relatives, key3=friends]
family
C. {key3=friends, key2=relatives, key1=family}
[EMPTY]
D. {key1=family, key2=relatives, key3=friends]
[EMPTY]
E. Compilation Error or Runtime Error 
Topic: Java HashMap Implementation

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

a)

Posted by Amruth Reddy    2014-03-28 12:36:25


ans is A..1st it wl print all keys n values as usal..nxt we are takng "key1" value into str which is family

Posted by Cherry Teegala    2014-03-28 13:24:05


ans is a bcz map class does not guarantee order of its elements and get(key1) gives the value so "family" is o/p 4r tat line

Posted by Pooja Singh    2014-03-28 13:25:46


a)

Posted by Manaswini Panuganti    2014-03-28 13:28:02


Congratulations Chota Viru. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-31 06:25:42


© meritcampus 2019

All Rights Reserved.

Open In App