• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sybex 829 - Introduction Ixii errata

 
Greenhorn
Posts: 4
  • Number of slices to send:
    Optional 'thank-you' note:
The explanation for answer 23 states that "Lambda expressions with one parameter are allowed to omit the parentheses around the parameter list, making option C correct".
However, when you look at option C, the parentheses are not omitted.

Additionally, it says "the return statement is optional when a single statement is in the body, making option F correct."
But option F has a return statement, and in fact has brackets around the body, meaning that the return statement is not optional at all.
 
Marshal
Posts: 79422
377
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Welcome to the Ranch

This question appears to be written not very clearly; somebody else got confused by it similarly last week: here, where you might find some more information. Please supply more information about the second question.
In both cases, I think there is confusion because in one case the (...) may be optional, and in another case because return may be optional.
In a λ, x ‑> 2 * x and (x) ‑> 2 * x are equivalent to each other, and both permissible.
Similarly, x ‑> 2 * x and x ‑> { return 2 * x; } are also equivalent to each other, and therefore both permissible.
Note that x ‑> return 2 * x without {...; } won't compile.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

A few hours ago, I wrote:. . . the (...) may be optional . . .

I think people are getting confused about the first question because they forget that, “allowed to omit,” doesn't mean, “must omit.”
 
Mitchell Coutinho
Greenhorn
Posts: 4
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks for the reply and for linking the other person's post, Campbell Ritchie.

Regarding answer C, what trips me up are not so much the rules regarding Lambda's and their various allowed syntaxes. Nor does the fact that answer C is correct surprise me. That much is quite clear to me.
Rather, what trips me up is the explanation of why answer C is correct.
Specifically, the explanation for answer C states that  "Lambda expressions with one parameter are allowed to omit the parentheses around the parameter list, making option C correct".

Sure, it is merely stated that parentheses are allowed to be omitted, and so you can infer that they may either be present or absent. In that sense, the explanation of this rule regarding lambda's contains no mistake.

But the fact that it is allowed to omit parentheses most certainly does not make option C correct. In fact, the fact that parentheses are allowed to be omitted has no relationship to the correctness of answer C at all, since in answer C the parentheses are not omitted.

Phrased differently, the stated rule is that parentheses may be omitted. The argument is that this rule makes option C correct.

But if this rule did not exist, option C would still be correct since it does not omit the parentheses.
How, then, can we argue that this rule is what makes option C correct?

This is the error that I attempted to point out.

As for answer F, it uses the following syntax

Similar to answer C, the explanation states that  "the return statement is optional when a single statement is in the body, making option F correct."
I suppose that technically, if the single statement is surrounded by brackets, then the return statement must be used. And if the brackets are omitted, then a return statement may not be used. So in that sense, one could argue that the return statement is somewhat optional.


But even if we apply such a lenient interpretation to the explanation, to state that answer F is correct specifically because the return statement is optional, when in fact the return statement is not optional for F due to the braces, strikes me as a mistake.

Ironically, the text then explains that for answer B, which was , that it is incorrect because a return statement must be used when braces surround the body.

The fact that the answer to B explains how the return statement is not optional when braces are present makes the mistaken explanation for F stand out all the more prominently.
The explanation to F says that the return statement is optional. The explanation to B says that it is not for a reason that also applies to F.

I, at least, find it hard to imagine that the authors intended the explanations to C and F to be this way.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

Mitchell Coutinho wrote:Thanks . . .

That's a pleasure

. . . what trips me up is the explanation of why answer C is correct. . . . allowed to omit the parentheses . . . they may either be present or absent. . . .

Yes, as you will have seen from the link I showed you, the (...) may be present or absent if you reduce the parameter list to exactly one token. As I said, the wording of the question seems not to be clear to everybody. The Java® Language Specification (=JLS) says,

If a lambda expression has exactly one formal parameter, and the parameter is specified by an identifier instead of a parameter specifier, then the parentheses around the identifier may be elided.

. . . and I don't think that is much clearer.

As for answer F . . . if the single statement is surrounded by brackets, then the return statement must be used. . . .

Thank you for showing us the additional information about question F .
I am in Britain, so I use the word “bracket” slightly differently from people in the USA, but please call {...} “braces.”
Let's see whether the JLS can help: try this section. Yes, the grammar says that a LambdaBody (=part right of the ‑>) may be an Expression or a Block. If your λ for F reads i ‑> i == 5, then the body is an expression. If you look up the grammar for Block (use link on that JLS page), you will see it may contain any number (including 0) of declarations and statements, but it is not allowed to contain plain simple expressions. If you put braces {...} around the body of the λ, then you are saying it mustn't contain plain expressions. You can however get it to compile by changing all the expressions to statements by prefacing them with return.
If no custom type has been shown, I believe the type of the λ in question F is either a Predicate<Integer, Boolean> or an IntPredicate.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

Yesterday, I wrote:. . . . Predicate<Integer, Boolean> . . . .

I think, if you look up the link, you will find I should have written Predicate<Integer>.
 
Mitchell Coutinho
Greenhorn
Posts: 4
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Yes, as you will have seen from the link I showed you, the (...) may be present or absent if you reduce the parameter list to exactly one token. As I said, the wording of the question seems not to be clear to everybody. The Java® Language Specification (=JLS) says,

If a lambda expression has exactly one formal parameter, and the parameter is specified by an identifier instead of a parameter specifier, then the parentheses around the identifier may be elided.


. . . and I don't think that is much clearer.



I appreciate the extra context. I did not find the explanation accompanying answer C to be all too confusing. I read the relevant section of the JLS that you were kind enough to reference. The section is also quite clear to me. I suspect that perhaps the rules regarding the use of parentheses were clear to Tim Mousaw as well when he submitted his feedback. The wording in his post suggests to me that the assertion made as to why answers C and F are correct are what he disagrees with, as I do.

I am in Britain, so I use the word “bracket” slightly differently from people in the USA, but please call {...} “braces.”


Hah, apologies. I'll try to keep that in mind

If no custom type has been shown, I believe the type of the λ in question F is either a Predicate<Integer, Boolean> or an IntPredicate.
...
I think, if you look up the link, you will find I should have written Predicate<Integer>.


Close enough! The question actually includes the functional interface in question. It was a Function<Integer, Boolean>.

In any case, I hope that this submission ends up being helpful for other readers taking the assessment test that could not find this in the errata on https://www.selikoff.net/ocp17/.
If this information is ever acknowledged as errata, I propose that Tim Mousaw is listed as the reporter for his initial post on the subject.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

Mitchell Coutinho wrote:. . . a Function<Integer, Boolean>.

Damn! I had that in the back of my mind as a possibility and didn't write it down

In any case, I hope that this submission ends up being helpful for other readers . . . .

I am sure it will. We find people coming back to threads many years old and finding useful information there.
 
author & internet detective
Posts: 41911
909
  • Number of slices to send:
    Optional 'thank-you' note:
Noted on explanation needing to be different
 
Mitchell Coutinho
Greenhorn
Posts: 4
  • Number of slices to send:
    Optional 'thank-you' note:

Jeanne Boyarsky wrote:Noted on explanation needing to be different



Great. Thanks for the verification!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |