What is the result?

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;
}
}
andList<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?

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;
}
}
andList<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, Beginning with Java:2]

B.
[Beginning with Java:2, A Guide to Java Tour:3]

C.
A compilation error occurs because the Book class does not override the abstract method compareTo().

D.
An Exception is thrown at run time.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


smh

smh

A.
code compiles if you correct AndList to List then answer is A.
[A Guide to Java Tour : 3.0, Beginning with Java : 2.0]