Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 207

Given the code fragment:

public class Foo {

public static void main (String [ ] args) {

Map unsortMap = new HashMap< > ( );

unsortMap.put (10, "z");

unsortMap.put (5, "b");

unsortMap.put (1, "d");

unsortMap.put (7, "e");

unsortMap.put (50, "j");

Map treeMap = new TreeMap (new

Comparator ( ) {

@Override public int compare (Integer o1, Integer o2) {return o1.compareTo

(o2); } } );

treeMap.putAll (unsortMap);

for (Map.Entry entry : treeMap.entrySet () ) {

System.out.print (entry.getValue () + " ");

}

}

}

What is the result?

    Correct Answer: B

    The code initializes a HashMap with various key-value pairs and then transfers the data to a TreeMap with a custom comparator that sorts the keys in natural ascending order. The for-each loop then iterates through the TreeMap, printing the values in the order of the sorted keys: 1, 5, 7, 10, and 50, which correspond to the values 'd', 'b', 'e', 'z', and 'j' respectively.

Discussion
jduarteOption: B

Answer B. Tested

steefaandOption: B

B is correct.

duydnOption: B

B is correct

WilsonKKerllOption: B

Answer is B.