exam questions

Exam 1z0-809 All Questions

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

Exam 1z0-809 topic 1 question 185 discussion

Actual exam question from Oracle's 1z0-809
Question #: 185
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
(o2); } } );
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: D 🗳️

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
steefaand
5 months, 4 weeks ago
Selected Answer: C
C is correct as o2.compareTo(o1) sorts keys in descending order.
upvoted 1 times
...
duydn
10 months, 2 weeks ago
Selected Answer: C
C is correct. Sort is DESC( I think this is a typo )
upvoted 1 times
...
mevlt
2 years, 5 months ago
The answer is C. I believe there is a typo in compare method. The second o2 should be o1. Otherwise it only prints single z which makes no sense.
upvoted 1 times
...
Huim
3 years, 2 months ago
a single z will be printed if with return o2.compareTo(o2); While a TreeMap cannot contain duplicate keys.
upvoted 3 times
...
Svetleto13
3 years, 2 months ago
Its B,tested with return o1.compareTo(o2).If you test it with return o2.compareTo(o2) answer is C.
upvoted 4 times
Svetleto13
3 years, 2 months ago
I meant return o2.compareTo(o1) for answer C
upvoted 2 times
...
...
fffff
3 years, 5 months ago
answer B: d b e z j tested; note the typo, it must be return o1.compareTo(o2);
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