1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 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?

Show Answer
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

14 comments
Sign in to comment
mvpVNOption: C
Jun 24, 2019

C (compiled & tested)

Ritesh_Option: C
Oct 10, 2019

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 ".

InnovationOption: C
Jan 13, 2020

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

akitravvOption: C
Apr 23, 2021

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

YasinGaberOption: C
Feb 12, 2022

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 () + " "); } } }

ThientvseOption: C
Feb 16, 2022

Tested

WilsonKKerllOption: C
Feb 21, 2022

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

MahdiHamdiiOption: C
Dec 14, 2022

C Tested!!

Eden0516Option: C
Jan 1, 2023

C (Tested)

r1muka5Option: C
Feb 27, 2023

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

duydnOption: C
Sep 21, 2023

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

steefaandOption: C
Feb 6, 2024

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

asdfjhfgjuaDCVOption: C
Mar 3, 2024

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 () + " "); } } }

DarGrinOption: C
May 16, 2024

Answer ist C