1Z0-809 practice question 15 of 25
Given the code fragments: public class Book implements Comparator { String name; double price; public Book () {} public Book(String name, double…
Full set: 196 questionsCollections.sort(List, Comparator) orders the list according to the supplied Comparator instance. new Book() is a Comparator whose compare method orders by the lexicographic value of name. Initial list order: “Beginning with Java”, “A Guide to Java Tour”. After sorting, “A Guide to Java Tour” precedes “Beginning with Java”. toString returns name + “:” + price; a double literal prints with “.0”. Result: [A Guide to Java Tour:3.0, Beginning with Java:2.0].
B. Shows the original (unsorted) order—contradicts comparator result.
C. Comparable is unnecessary; Collections.sort(List, Comparator) needs only a Comparator.
D. No exception is thrown; code compiles and runs normally.
1. Oracle Java SE 8 API, java.util.Collections, method sort(List, Comparator) — “sorts the specified list according to the order induced by the specified comparator.”
2. Oracle Java SE 8 API, java.util.Comparator, method compare(T, T).
https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-T-T-
3. Java Language Specification §15.21.2, String comparison rules (lexicographic ordering).
https://docs.oracle.com/javase/specs/jls/8/html/jls-15.html#jls-15.21.2
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
