• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

OCP Java SE 11 Study Guide - seems error in answer to review question 5 of chapter 6 Lambdas...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(entire copy of book deleted)
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops, posted before I got to specify.

Seems answer E is also correct. Not just B and D.
I was able to use
 (s1) -> s.isEmpty()
as a lambda expression passed to a function of Predicate<String>

The case is where s1 is unused and s is accessed via closure - s may be a variable referenced from the lambda body
 
Marshal
Posts: 28305
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you also post the question you're asking about and the possible answers?

(Originally you posted a download of the entire book. I don't know whether that counts as piracy, maybe not, but it's certainly very hard to find the question you're asking about. Just post the question.)
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies for the unnecessary book post, accidental.
I wondered if the "Book" link above offered the possibility of grabbing the text section.
So I tried it and book was posted.

P 242
5. Which of the following lambda expressions can be passed to a function of Predicate<String> type? (Choose all that apply.)

A.  () -> isEmpty()
B.  s -> s.isEmpty()
C.  String s -> s.isEmpty()
D.  (String s) -> s.isEmpty()
E.  (s1) -> s.isEmpty()
F.   (s1, s2) -> s1.isEmpty()

The review answers indicate that B, D are the correct answers.
E works just fine as via local and instance variable usage:

 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless explicitly stated by the question, you may not assume that your current scope contains any variables you can use.
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan, you point the way to fixing the question.
The problem is that no code context is supplied at all.
Any answer involving specific code is unsafe here.

Your assertion applies equally to s as it does to s1, neither can be assumed to be in scope.
Accordingly, B and D would also be incorrect.

The larger problem however seems to be in the rationale provided in the review answer, I should have posted it. my apologies.
"Option E is incorrect because then name used in the parameter list does not match the name used in the body"

This seems clearly incorrect.
To fix it, provide the code having only s and no s1 in the enclosing scope, so it is clearly illegal.

However, in keeping with the "trick you" spirit of the exam, I would be inclined to provide a code example with s1 in the enclosing scope and allow E to be one of the correct answers ;-)
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops, my suggestion is wrong way around.
I meant provide no s in the enclosing scope, meaning s is the only means to access a value, making s1 an illegal lambda parameter.
Alternately, provide an enclosing s and make E a valid answer.
Either way, provide some question code when the answer references actual code.
 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only contextual code you may assume is the following:

With the side note that you may not assume that the name "filter" is defined; it is just an example name.

I derived this context from the question:

a function of Predicate<String> type


The question is a bit poorly worded, but its intention is relatively clear.


Walter Rockett wrote:Accordingly, B and D would also be incorrect.


No. Answers B and D don't refer to any contextual code. The identifiers used in the body of those lambda expressions are declared in their respective parameter lists.


The larger problem however seems to be in the rationale provided in the review answer, I should have posted it. my apologies.
"Option E is incorrect because then name used in the parameter list does not match the name used in the body"

This seems clearly incorrect.


It's not. Option E accesses a variable s, which is not defined. Therefore, option E is incorrect.

This is different from option B, for instance. Option B also accesses a variable s, but it is declared at the start of the lambda expression, so it is valid code.

However, in keeping with the "trick you" spirit of the exam, I would be inclined to provide a code example with s1 in the enclosing scope and allow E to be one of the correct answers ;-)


While I do think that the exams appear to contain many tricky questions that really serve no educational purpose, this is not one of those questions. I would say this is one of the more straightforward questions I've come across.

Either way, provide some question code when the answer references actual code.


Yes, it would be better if the question had given some example code in which to insert the correct answer. However, in this case I think the code can be easily deduced from the problem statement.
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan for the clarification.
I understand now that options B and D control their own scope within the lambda and so are valid isolated code only in the absence of any other enclosing code.

However, please alow me to assess the context you infer is as follows




Did you mean the following context:



 
Stephan van Hulst
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

The ellipses simply mean that there could be any code there, BUT you MAY NOT make any assumptions about that code. Importantly, you may not assume that a variable named s or s1 could be declared there. The correct answers must be correct IN ALL CIRCUMSTANCES.
 
Walter Rockett
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunatey, one of those circumstances is that code defining a variable s _could_ be present.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic