exam questions

Exam 1z0-809 All Questions

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

Exam 1z0-809 topic 1 question 37 discussion

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

Given the code fragments:
public class Book implements Comparator<Book> {
String name;
double price;
public Book () {}
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public int compare(Book b1, Book b2) {
return b1.name.compareTo(b2.name);
}
public String toString() {
return name + ":" + price;
}
}
and
List<Book>books = Arrays.asList (
new Book ("Beginning with Java", 2),
new book ("A Guide to Java Tour", 3)
);
Collections.sort(books, new Book());
System.out.print(books);
What is the result?

  • A. [A Guide to Java Tour:3.0, Beginning with Java:2.0]
  • B. [Beginning with Java:2.0, A Guide to Java Tour:3.0]
  • C. A compilation error occurs because the Book class does not override the abstract method compareTo().
  • D. An Exception is thrown at run time.
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

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
Innovation
Highly Voted 5 years ago
A is correct
upvoted 10 times
...
Svetleto13
Highly Voted 3 years, 9 months ago
A,tested
upvoted 5 times
Svetleto13
3 years, 8 months ago
If we change places to that return b2.name.compareTo(b1.name) answer is B.
upvoted 3 times
...
...
alboy68
Most Recent 6 months ago
Selected Answer: A
A is correct
upvoted 1 times
...
asdfjhfgjuaDCV
11 months ago
A is correct import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Book implements Comparator<Book> { String name; double price; public Book() { } public Book(String name, double price) { this.name = name; this.price = price; } public int compare(Book b1, Book b2) { return b1.name.compareTo(b2.name); } public String toString() { return name + ":" + price; } public static void main(String[] args) { List<Book> books = Arrays.asList( new Book("Beginning with Java", 2), new Book("A Guide to Java Tour", 3) ); Collections.sort(books, new Book()); System.out.print(books); } }
upvoted 1 times
...
steefaand
12 months ago
Selected Answer: A
Answer is A as b1.name.compare(b2.name) sorts elements by name in ascending order.
upvoted 2 times
...
r1muka5
1 year, 11 months ago
Selected Answer: A
Correct answer is A.
upvoted 1 times
...
ayzo
4 years ago
Correct answer is A but my question is on the exam which answer to choose? cause it might happen that the system is programmed to select D as the correct answer even if you choose A you become incorrect? class Testing implements Comparator<Testing> { String name; double price; public Testing () {} public Testing(String name, double price) { this.name = name; this.price = price; } public int compare(Testing b1, Testing b2) { return b1.name.compareTo(b2.name); } public String toString() { return name + ":" + price; } public static void main (String[] args){ List<Testing>books = Arrays.asList ( new Testing ("Beginning with Java", 2), new Testing ("A Guide to Java Tour", 3) ); Collections.sort(books, new Testing()); System.out.print(books); } }
upvoted 2 times
...
pul26
4 years, 1 month ago
The correct answer is A. Tested import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class Book implements Comparator<Book> { String name; double price; public Book () {} public Book(String name, double price) { this.name = name; this.price = price; } public int compare(Book b1, Book b2) { return b1.name.compareTo(b2.name); } public String toString() { return name + ":" + price; } } public class CollectionsSortBook { public static void main(String[] args) { List<Book> books = Arrays.asList ( new Book ("Beginning with Java", 2), new Book ("A Guide to Java Tour", 3) ); Collections.sort(books, new Book()); System.out.print(books); } }
upvoted 2 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