• Post Reply Bookmark Topic Watch Topic
  • New Topic

Functional programming: On the ambiguous use of the word 'result' (Sybex)  RSS feed

 
Ranch Hand
Posts: 50
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In chapter 4, Functional Programming, question #18 reads:


18. What is the result of the following?


A. 1
B. 2
C. 2.4
D. 1.2 and 2.4
E. There is no output.
F. The code does not compile.
G. An exception is thrown.



The correct answer is D. I'd have said that the result is clearly 1 while the printing of the numbers is the side effect.

If we refer to the definition of 'result':


1 : something that results as a consequence, issue, or conclusion; also : beneficial or tangible effect : fruit
2 : something obtained by calculation or investigation



We find that the question could have been written with both definitions in mind. I think the question should have been worded so that it explicitly asked for the output or the outcome of the execution of the code, as in the context of a programming language the reader has a tendency to interpret 'result' by its second definition.

MOREOVER

If we take for granted that we are being asked for the output, and more specifically, for the console output, then the answer D is incorrect as well, since the " and " in "1.2 and 2.4" is never printed.


I'm sorry if I'm being picky here but the questions are tricky as well.
 
Sheriff
Posts: 57818
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us which book it is as well as the question number. That reduces problems about copyright and makes it easier for us to find the original.

I think the book means by result the “tangible effect” of that program, so that corresponds to one of your definitions. As you doubtless know, Streams run on lazy execution, but all elements are processed until there is some method which restricts the Stream. If you format the code differently, and remove the redundant assgnment,... you can see that the peek call precedes the filter() call, so both elements are handled by the Stream in lines 1‑2. Only on line 3 is there a one‑element Stream. The count() call doesn't produce a result of 1L; it leaves 1L on the stack, whence it subsequently disappears when the method completes. The 1L is therefore regarded as unreachable and cannot be used as a result.
Since 1.2 and 2.4 are printed to the console, I find it surprising that you think that isn't part of the result. Also what is all this about quote marks: I can see no quote marks anywhere else in your question?
 
John Schubert
Ranch Hand
Posts: 50
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please tell us which book it is as well as the question number. That reduces problems about copyright and makes it easier for us to find the original.



Sorry it is the OCP Java SE 8 Certified Programmer  Study Guide, chapter 4 (I thought I wrote it in the title, but apparently I didn't)

Campbell Ritchie wrote: I find it surprising that you think that isn't part of the result.



When I read result, and there is a terminal operation like count, I immediately associate it to a possible correct answer, because it is the result of the stream, and a stream is a computation, fitting the second definition more. The two numbers do not look like THE result, because they are two things (the printing of the two numbers might have been a better candidate for a result). The "and" particle was never printed to the console, so that confused me even more. Maybe if the numbers were collected into a list they could have been considered a result by me.

All this confusion would have not existed had the question asked for the output instead (after all we come from a series of previous questions asking for the output as well).
Or maybe keep the question as is but rewrite the correct option as:
D. 1.2 and 2.4 are printed




 
author & internet detective
Marshal
Posts: 37896
594
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:result" means something that happens. Ex: what is printed due to running the code. Granted side effects are bad. But if they happen, they are still part of the result.

Also, remember that the exam questions are written in normal English language; just like the book. Try not to read into the meanings of words lest you get confused.
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Schubert wrote:

Campbell Ritchie wrote:Please tell us which book it is as well as the question number. That reduces problems about copyright and makes it easier for us to find the original.



Sorry it is the OCP Java SE 8 Certified Programmer  Study Guide, chapter 4 (I thought I wrote it in the title, but apparently I didn't)

Campbell Ritchie wrote: I find it surprising that you think that isn't part of the result.



When I read result, and there is a terminal operation like count, I immediately associate it to a possible correct answer, because it is the result of the stream, and a stream is a computation, fitting the second definition more. The two numbers do not look like THE result, because they are two things (the printing of the two numbers might have been a better candidate for a result). The "and" particle was never printed to the console, so that confused me even more. Maybe if the numbers were collected into a list they could have been considered a result by me.

All this confusion would have not existed had the question asked for the output instead (after all we come from a series of previous questions asking for the output as well).
Or maybe keep the question as is but rewrite the correct option as:
D. 1.2 and 2.4 are printed






I think the reasoning in the first answer to your question is plausible. However, I am still with you on this.

For me, the result is the end of the computation , here a chain of methods. That the result is thrown away is immaterial, it's still the result.

It's not a well worded English language question; that's how I see it, Why should what goes to strandard IO be privileged as "the result" ? Because it stays on the screen ? Is that always the case? What if we changed the program so count were saved to a variable, but then threw that variable away? What is the result now? What if we change it again to save the variable containing count instead of throwing it away?  What is the result then, or is there two now - one opn the screen and one in memory?

Lots of corporate products are both created and reviewed by people for whom English is not their first langauge. In that case, it's possible to the common connotation of a word gets lost. That's in no way a dig at anyone, just a hazard for anyone, including myself, who endeavors to be precise in a second language.

Result for me is definitely the end result of a sequential chain of processing. If result is actually defined as something different, then I'll get used to it, like I got used to starting counting at 0 and not 1.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37896
594
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
I do understand what you are saying. However, on the real exam, "result" is anything that gets output or any exceptions that get thrown.

Since the purpose of the study guide is to prepare you for the exam, I want to ensure you don't get a question wrong because you interpret "result" differently!

In the real world, relying on side effects in peek() is a horrible practice and you are completely correct!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!