About Question enthuware.ocpjp.i.v11.2.3067 :

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

Moderator: admin

Post Reply
demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

About Question enthuware.ocpjp.i.v11.2.3067 :

Post by demetrio »

Question: Which of the following lambda expressions can be used to implement a Function<Integer, String> ?

WRONG OPTION) a -> Integer::toHexString

Integer::toHexString is a valid method reference but is not suitable as a body of a lambda expression because it is not possible to determine the argument that is to be passed to this method from the context. The given lambda expression will be converted into an anonymous class like this:

new Function(){    public String apply(Integer a){        return Integer::toHexString;  <- The method reference cannot be coverted into an expression that returns a String at this point.    }

---

CORRECT OPTION) Integer::toHexString

This method reference can be coverted to a functional interface implementation because there is only one possible value that can be passed as an argument to toHexString, which is the argument passed to the Funtion's apply method. Something like this:

new Function(){    public String apply(Integer arg){        return Integer.toHexString(arg);    } }


Observe the difference between how a method reference and a lambda expression are handled. When you use a method reference, it is "converted" into a method body. But when you use a lambda expression, the expression given in the lamba body is used as it is in the method body.

My issue:

I can´t figure out why return Integer::toHexString; is in first case and return Integer.toHexString(arg);. I am trying to find a concept or theory I could apply in exam if similar question pops up. Additionally, I understand that both case are method reference converted to method body since I see "::" (double colon). If so, what isn't "a -> Integer::toHexString" also converted to Integer.toHexString(a)?

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

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Post by admin »

The first case ( a -> Integer::toHexString ) is a lambda expression (which uses a method reference), while the second case ( Integer::toHexString ) is not a lambda expression. It is only a method reference. Both are converted into an inner classes but in different ways. This is what the explanation to the 3rd options highlights:
Observe the difference between how a method reference and a lambda expression are handled. When you use a method reference, it is "converted" into a method body. But when you use a lambda expression, the expression given in the lamba body is used as it is in the method body.
That is why a -> Integer::toHexString doesn't work but Integer::toHexString works.

(BTW, the problem statement is imprecise and should be fixed. It should say, "Which of the following expressions...", instead of "Which of the following lambda expressions...")
If you like our products and services, please help us by posting your review here.

Dani1515
Posts: 9
Joined: Mon Feb 17, 2020 12:10 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Post by Dani1515 »

hi, I am preparing for the exam with the book "Java SE 11 Programmer 1 Exam Fundamentals 1Z0-815" from Mr. H. deshmukh, this Functional Interface is not contained in the chapter about lambda expressions, my question is: Can I nevertheless expect a question about that f. I. on the exam or not?
In the mock exams are also questions about Wrapper Classes, but they are not anymore on the OCP Java 11 Part 1 exam (source: the mentioned book), so I am little bit confused.

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

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Post by admin »

Hi,
The java.util.function.Function is not officially on the exam but we have unconfirmed reports that some people got a question that referred to this interface. That is why it is not in the book but it is there in the mock exams (only a couple of questions). Our suggestion is to just go through it once if you have time. We may add it in the book later after it is confirmed from multiple sources.

Wrapper classes are are covered in the book (in detail) as well as in the mock exams. There is no confusion on this topic. You have to study it.

Please note that mock exams are more dynamic than the book and so you may find a few questions in the mock exams that are newly added (due to feedback received from users) but not in the book. Eventually, such things will be added in the book also.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dimitrilc
Posts: 35
Joined: Sat Jun 06, 2020 4:51 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Post by dimitrilc »

Yeah the double colon operator "::" was not mentioned anywhere in the book. I got this question wrong, too.

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

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Post by admin »

As explained above:
real exam < book < mock exams.

Book will cover some more than the real exam. Mock exams will cover a bit more than the book.
Also see the motivation here: viewtopic.php?f=2&t=5562
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests