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

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 o2.compareTo

(o1); } } );

treeMap.putAll (unsortMap);

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

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

}

}

}

What is the result?

    Correct Answer: C

    The code defines a TreeMap with a custom Comparator that sorts the keys in descending order by returning o2.compareTo(o1) in the compare method. When the unsorted HashMap entries are inserted into the TreeMap, they are sorted in descending order based on their keys. The keys in descending order are 50, 10, 7, 5, and 1, corresponding to the values 'j', 'z', 'e', 'b', and 'd', respectively. Thus, the output will be 'j z e b d'.

Discussion
mvpVNOption: C

C (compiled & tested)

Ritesh_Option: C

Answer is "j z e b d" .If the compare method is returning o1.compareTo(o2) then the answer is "d b e z j ".

DarGrinOption: C

Answer ist C

asdfjhfgjuaDCVOption: C

C is the answer: import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.function.IntFunction; import java.util.function.IntUnaryOperator; import java.util.stream.IntStream; class Vehicle { public static void main (String [ ] args) { Map<Integer, String> unsortMap = new HashMap< > ( ); unsortMap.put (10, "z"); unsortMap.put (5, "b"); unsortMap.put (1, "d"); unsortMap.put (7, "e"); unsortMap.put (50, "j"); Map<Integer, String> treeMap = new TreeMap <Integer, String> (new Comparator<Integer> ( ) { @Override public int compare (Integer o1, Integer o2) {return o2.compareTo (o1); } } ); treeMap.putAll (unsortMap); for (Map.Entry<Integer, String> entry : treeMap.entrySet () ) { System.out.print (entry.getValue () + " "); } } }

steefaandOption: C

Answer is C since o2.compareTo(o1) sorts keys in descending order.

duydnOption: C

C is correct, TreeMap need provide a Comparator, sort is DESC -> C

r1muka5Option: C

Correct answer is C because of overriden compare() method which says to compare in DESC order

Eden0516Option: C

C (Tested)

MahdiHamdiiOption: C

C Tested!!

WilsonKKerllOption: C

Correct answer is C. Because @Override public int compare (Integer o1, Integer o2) { return o2.compareTo(o1); } is decrease.

ThientvseOption: C

Tested

YasinGaberOption: C

Answer is C (compiled and tested as following): package birdie; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.function.IntFunction; import java.util.function.IntUnaryOperator; import java.util.stream.IntStream; class Vehicle { public static void main (String [ ] args) { Map<Integer, String> unsortMap = new HashMap< > ( ); unsortMap.put (10, "z"); unsortMap.put (5, "b"); unsortMap.put (1, "d"); unsortMap.put (7, "e"); unsortMap.put (50, "j"); Map<Integer, String> treeMap = new TreeMap <Integer, String> (new Comparator<Integer> ( ) { @Override public int compare (Integer o1, Integer o2) {return o2.compareTo (o1); } } ); treeMap.putAll (unsortMap); for (Map.Entry<Integer, String> entry : treeMap.entrySet () ) { System.out.print (entry.getValue () + " "); } } }

akitravvOption: C

import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class MyClass { public static void main(String[] args) { //HashMap is an unsorted Map in Java, neither key nor value is sorted. Map<Integer, String> unsortMap = new HashMap<>(); unsortMap.put(10, "z"); unsortMap.put(5, "b"); unsortMap.put(1, "d"); unsortMap.put(7, "e"); unsortMap.put(50, "j"); Map<Integer, String> treeMap = new TreeMap<Integer, String>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //returns values in descending order return o2.compareTo(o1); } }); treeMap.putAll(unsortMap); for (Map.Entry<Integer, String> entry : treeMap.entrySet()) { System.out.print(entry.getValue() + " "); } } } //ANSWER: C. j z e b d

InnovationOption: C

Map<Integer, String> treeMap = new TreeMap <Integer, String> ( (Comparator<? super Integer>) new Comparator<Integer> ( ) { @Override public int compare (Integer o1, Integer o2) { return o2.compareTo(o1); } } ); answer is C