This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

OCP Oracle Certified Professional Java SE8 Programmer II Study Guide - Error on test question 22  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is:
What is the result of executing the following program?

Answer options:

A. It prints 0 1 2 3 4
B. It prints 1 2 3 4 5
C. It prints null null null null null
D. It hangs indefinitely at runtime.
E. The output cannot be determined.
F. The code will not compile because of line n1.
G. The code will not compile because of line n2.

The correct answer is:

F. The key to solving this question is to remember that the execute() method returns
void, not a Future object. Therefore, line n1 does not compile and F is the correct answer.
If the submit() method had been used instead of execute(), then C would have been the
correct answer, as the output of submit(Runnable) task is a Future<?> object which can
only return null on its get() method.

I'm absolutely agreed with the answer as I answer it the same. What is confuses me is a propagated alternative: in case of using of submit() method, passed lambda expression  () -> counter++ would be evaluated as pure Callable not Runnable (in fact this lambda can also be evaluated as Runnable due to compiler contract, the same as Function lambda can be evaluated as Consumer if required) and program will end up with the answer A.
 
author
Posts: 4106
24
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes, the answer is correct, but the alternate text is wrong.  I believe in the original version, the lambda was actually:



Somewhere along the way, the {} was dropped, making the alternate answer incorrect.  Thanks for finding this, we will update the text!
 
You showed up just in time for the waffles! And this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!