Menu
Topics Index
...
`

Districts


If you need explanation Read this topic

If you need Answer Take test on this topic

Districts
What will be the output of the following program?
import java.util.*;
public class Districts {
    public static void main(String args[]) {
        SortedMap map = new TreeMap();
        map.put("Kadapa", 1);
        map.put("Ananthapur", 2);
        map.put("Kurnool", 3);
        map.put("Chittoor", 4);
        System.out.println("first value: " + map.firstKey());
        System.out.print("last value: " + map.lastKey());
    }
}

A. first value: Kadapa
last value: Chittoor
B. first value: Ananthapur
last value: Kurnool
C. first value: Ananthapur
last value: Chittoor
D. first value: Chittoor
last value: Kurnool
E. Compilation Error or Runtime Error
Topic: TreeMap In Java - java.util.TreeMap

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.

Ans is B.
because in TreeMap the elements are stored according to keys in ascending order.
so firstkey will be anantapur
and lastkey will be kurnool.

Posted by Gopinath Manchikanti    2014-08-08 12:52:27


Answer is A firstkey method returns the kadapa and the lastkey method returns chittor as kadapa is inserted as at first position i.e 1 and chittor is inserted at last position i.e 4

Posted by Sai Avinash    2014-08-08 12:53:34


Ans is B first parameter of put method represents the key and second parameter represents the value. TreeMap stores the entries in order based on key only.

Posted by Krishna Chandar P    2014-08-09 17:44:46


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

Posted by Merit Campus    2014-08-11 04:34:53


© meritcampus 2019

All Rights Reserved.

Open In App