Java OCA OCP Practice Question 3282

Question

Consider the following program and predict the output:

import java.util.*;

public class Main {
     public static void main(String []args) {
             Map<Integer, String> map = new TreeMap<Integer, String>();
             map.put(5, "5");
             map.put(10, "10");
             map.put(3, "3");
             map.put(5,"25");
             System.out.println(map);
     }//  w w w.  j a  v a2s .  c om
}
  • a) {5=5, 10=10, 3=3, 5=25}
  • b) {10=10, 3=3, 5=25}
  • c) {3=3, 5=5, 5=25, 10=10}
  • d) {3=3, 5=25, 10=10}
  • e) {3=3, 5=5, 10=10}


d)

Note

TreeMap is a Map;-a value is stored against a key, and the elements are sorted based on the key.

Option c) is not possible since two values cannot exist for a key.

In a Map, keys are sequential, so options a) and b) are not possible.

Option e) is also not correct since you have overwritten the value 25 against key 5, which is not captured by option e).




PreviousNext

Related