exam questions

Exam 1z0-809 All Questions

View all questions & answers for the 1z0-809 exam

Exam 1z0-809 topic 1 question 32 discussion

Actual exam question from Oracle's 1z0-809
Question #: 32
Topic #: 1
[All 1z0-809 Questions]

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 Ford 10124 BMW
  • B. 10124 BMW 10123 Ford
  • C. A compilation error occurs.
  • D. A ClassCastException is thrown at run time.
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
jduarte
Highly Voted 3 years, 2 months ago
answer is D. java.lang.ClassCastException Vehicle cannot be cast to java.lang.Comparable
upvoted 5 times
...
shivkumarx
Most Recent 4 months ago
Selected Answer: 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
upvoted 1 times
...
asdfjhfgjuaDCV
4 months, 1 week ago
Answer is D if this.vno = vno,; is in correct format
upvoted 1 times
...
steefaand
5 months, 2 weeks ago
Selected Answer: D
D is correct since Vehicle doesn't implement Comparable.
upvoted 1 times
...
iSnover
5 months, 3 weeks ago
Selected Answer: 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
upvoted 1 times
...
r1muka5
1 year, 4 months ago
Correct answer is D. Missing implementing Comparable interface
upvoted 1 times
...
WilsonKKerll
2 years, 4 months ago
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; } }
upvoted 1 times
...
bnagaraja9099
3 years, 2 months ago
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')
upvoted 2 times
...
Innovation
4 years, 6 months ago
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); } }
upvoted 3 times
shivkumarx
4 months ago
this is incorrect, you can run the code without implementing the interface. Correct answer is D
upvoted 2 times
...
...
M_Jawad
4 years, 6 months ago
ClassCastException is thrown at run time
upvoted 2 times
...
adnano1234
4 years, 6 months ago
The answer is C: ClassCastException
upvoted 3 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago