Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 37

Given the code fragments:

public class Book implements Comparator {

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

Listbooks = 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?

    Correct Answer: A

    The given code defines a class Book that implements Comparator<Book>. The compare method in the Book class compares two Book objects based on their names in ascending order. The code snippet then creates a list of Book objects and sorts them using the compare method, resulting in the list being sorted by book titles. Hence, the correct output will be [A Guide to Java Tour:3.0, Beginning with Java:2.0].

Discussion
InnovationOption: A

A is correct

Svetleto13Option: A

A,tested

Svetleto13

If we change places to that return b2.name.compareTo(b1.name) answer is B.

ayzoOption: A

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); } }

pul26Option: A

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); } }

asdfjhfgjuaDCVOption: A

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); } }

steefaandOption: A

Answer is A as b1.name.compare(b2.name) sorts elements by name in ascending order.

r1muka5Option: A

Correct answer is A.