Exam 1z0-809 All QuestionsBrowse all questions from this exam
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 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: D

    The code attempts to use a TreeSet to store Vehicle objects. TreeSet requires elements to be comparable, either by implementing the Comparable interface or providing a Comparator. The Vehicle class does not implement Comparable, and no Comparator is provided, leading to a ClassCastException at runtime due to the inability to compare Vehicle objects.

Discussion
jduarteOption: D

answer is D. java.lang.ClassCastException Vehicle cannot be cast to java.lang.Comparable

InnovationOption: C

C is correct in order for the code to run, the class vehicle needs to implement comparable interface, nad override the compareTo method. class Vehicle implements Comparable<Vehicle>{ int vno; String name; public Vehicle (int vno, String name) { this.vno = vno; this.name = name; } public String toString () { return vno + ":" + name; } @Override public int compareTo(Vehicle o) { return this.name.compareTo(o.name); } }

shivkumarx

this is incorrect, you can run the code without implementing the interface. Correct answer is D

adnano1234Option: C

The answer is C: ClassCastException

bnagaraja9099Option: D

D: Exception in thread "main" java.lang.ClassCastException: class test.Vehicle cannot be cast to class java.lang.Comparable (test.Vehicle is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

M_JawadOption: D

ClassCastException is thrown at run time

shivkumarxOption: D

There is no compilation issues, so you do not need to implement the Comparable interface in order for the code to run You will encounter a runtime exception stating the Vehicle class can not be cast to Comparable

asdfjhfgjuaDCVOption: D

Answer is D if this.vno = vno,; is in correct format

steefaandOption: D

D is correct since Vehicle doesn't implement Comparable.

iSnoverOption: C

The answer is C, the reason being that in the line "this.vno = vno,;" the variable has "," at the end, this causes the code not to compile. If the line is corrected to "this.vno = vno;" the answer would be D because java.lang.ClassCastException Vehicle cannot be cast to java.lang.Comparable

r1muka5Option: D

Correct answer is D. Missing implementing Comparable interface

WilsonKKerllOption: C

Change Vehicle class -->>>> class Vehicle implements Comparable<Vehicle> { int vno; String name; public Vehicle(int vno, String name){ this.vno = vno; this.name = name; } public String toString() { return vno + name; } @Override public int compareTo(Vehicle o) { if (vno > o.vno) return 1; else return 0; } }