About Question enthuware.ocpjp.v11.2.1862 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
fawcette01
Posts: 1
Joined: Wed Feb 02, 2022 6:58 pm
Contact:

About Question enthuware.ocpjp.v11.2.1862 :

Post by fawcette01 »

What is going on here?
The list will not be printed, only the object references. Or should we assume there is a toString()?

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

There is no need to assume. All objects have a toString method already. It may not print the value of the fields of the object, but it does print a unique string for that object (No, it doesn't print object reference. It prints the hashcode).

Further, what the code actually prints is not really relevant here because all it says is that the list is printed, which it does. It doesn't claim anything about the actual format or content of what is printed.

mansi29feb
Posts: 4
Joined: Thu Aug 04, 2022 10:23 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by mansi29feb »

The book class is not implementing Comparable interface then how the compareTo function is working on line //1

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

Book doesn't implement Comparable. But the code passes a Comparator instance that is able to compare two Book objects.
This Comparator uses the compareTo method of String class.
Observe the lambda used to create the Comparator. It calls b1.getGenre().compareTo and not b1.compareTo.

mansi29feb
Posts: 4
Joined: Thu Aug 04, 2022 10:23 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by mansi29feb »

Thanks for the explaination. Got it.

nnavlani
Posts: 3
Joined: Sat Aug 02, 2025 11:21 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by nnavlani »

Comparator is supposed to use compare and NOT compareTo, then why is the option "Code will fail to compile because of code at //1." NOT the correct answer ?

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

Why I think it should fail compilation?
The given lambda expression correctly provides an implementation of the compare method.

nnavlani
Posts: 3
Joined: Sat Aug 02, 2025 11:21 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by nnavlani »

But the method called in the Comparator is compareTo, and not compare? So does it mean that the Comparator Book has a wrong call there to compareTo just to confuse us?

admin
Site Admin
Posts: 10419
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.1862 :

Post by admin »

Not sure what is causing the confusion. Comparator is an interface, it doesn't call anything. It has a method named compare, and the code in the compare method is free to implement the logic required for comparison as it deems fit. In this case, the code in the compare method utilizes the compareTo method, which is already available in String.
Logically, there is no difference between Comparator and Comparable and no difference between their methods compare and compareTo. Comparator compares two given objects, while Comparable compares "this" object with another given object.

Post Reply

Who is online

Users browsing this forum: maehooo and 15 guests