exam questions

Exam 1z0-809 All Questions

View all questions & answers for the 1z0-809 exam

Exam 1z0-809 topic 1 question 4 discussion

Actual exam question from Oracle's 1z0-809
Question #: 4
Topic #: 1
[All 1z0-809 Questions]

Given the code fragment:
public class Foo {
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 () + " ");
}
}
}
What is the result?

  • A. A compilation error occurs.
  • B. d b e z j
  • C. j z e b d
  • D. z b d e j
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
mvpVN
Highly Voted 5 years, 6 months ago
C (compiled & tested)
upvoted 8 times
...
MPignaProTech
Most Recent 1 month, 4 weeks ago
Selected Answer: C
C is good
upvoted 1 times
...
DarGrin
7 months, 4 weeks ago
Selected Answer: C
Answer ist C
upvoted 1 times
...
asdfjhfgjuaDCV
10 months, 1 week ago
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 () + " "); } } }
upvoted 1 times
...
steefaand
11 months, 1 week ago
Selected Answer: C
Answer is C since o2.compareTo(o1) sorts keys in descending order.
upvoted 1 times
...
duydn
1 year, 3 months ago
Selected Answer: C
C is correct, TreeMap need provide a Comparator, sort is DESC -> C
upvoted 1 times
...
r1muka5
1 year, 10 months ago
Correct answer is C because of overriden compare() method which says to compare in DESC order
upvoted 1 times
...
Eden0516
2 years ago
Selected Answer: C
C (Tested)
upvoted 1 times
...
MahdiHamdii
2 years ago
Selected Answer: C
C Tested!!
upvoted 1 times
...
WilsonKKerll
2 years, 10 months ago
Correct answer is C. Because @Override public int compare (Integer o1, Integer o2) { return o2.compareTo(o1); } is decrease.
upvoted 1 times
...
Thientvse
2 years, 10 months ago
Selected Answer: C
Tested
upvoted 1 times
...
YasinGaber
2 years, 11 months ago
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 () + " "); } } }
upvoted 1 times
...
akitravv
3 years, 8 months ago
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
upvoted 1 times
...
Innovation
4 years, 12 months ago
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
upvoted 1 times
...
Ritesh_
5 years, 3 months ago
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 ".
upvoted 3 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago