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

Given:

class Vehicle implements Comparable{

int vno;

String name;

public Vehicle (int vno, String name) {

this.vno = vno,;

this.name = name;

}

public String toString () {

return vno + ":" + name;

}

public int compareTo(Vehicle o) {

return this.name.compareTo(o.name);

}

and this code fragment:

Set vehicles = new TreeSet <> ();

vehicles.add(new Vehicle (10123, "Ford"));

vehicles.add(new Vehicle (10124, "BMW"));

System.out.println(vehicles);

What is the result?

    Correct Answer: B

    The result is [10124:BMW, 10123:Ford]. The implementation of the compareTo method sorts the vehicles by the name field in ascending order (lexicographical order). Since 'BMW' comes before 'Ford' alphabetically, the BMW vehicle will be listed before the Ford vehicle in the TreeSet.

Discussion
jduarteOption: B

Answer is B. Tested

_Ismail_Option: B

B- tested

asdfjhfgjuaDCVOption: B

b is the correct answer

steefaandOption: B

B is correct, tested. List is sorted by name, not id.

duydnOption: B

Answer is B. Sort is ASC

samtash1034Option: D

Answer is D,tested

Svetleto13Option: B

B,tested.If we check the code with return o.name.compareTo(this.name); answer is A.