1z0-809 question 32 discussion

View all Java SE 8 Programmer II Exam here
back to oracle forum

Question 32

Given:
class Vehicle {
int vno;
String name;
public Vehicle (int vno, String name) {
this.vno = vno,;
this.name = name;
}
public String toString () {
return vno + : + name;
}
}
and this code fragment:
Set<Vehicle> vehicles = new TreeSet <> ();
vehicles.add(new Vehicle (10123, Ford));
vehicles.add(new Vehicle (10124, BMW));
System.out.println(vehicles);
What is the result?

  • A. 10123 Ford10124 BMW
  • B. 10124 BMW10123 Ford
  • C. A compilation error occurs.
  • D. A ClassCastException is thrown at run time.
Answer:

D

User Votes:
A
50%
B
50%
C 1 votes
50%
D
50%
Discussions
0 / 1000
vidhya
6 months, 3 weeks ago

here , the vehical class is not implementing comparator interface and didnt define its compareto method or we can use comparator<vehicles> to add vehicals in the treeset.so while adding the vehicles in the tree set it results in compilation error