• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Question on Concurrency OCP Oracle Certified Professional Java SE8 Programmer II Study Guide  RSS feed

 
Ranch Hand
Posts: 72
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

This question is regarding an answer on a question in one of the chapter review questions in the book: OCP Oracle Certified Professional Java SE8 Programmer II Study Guide by Jeanne Boyarsky and Scott Selikoff.

The question goes as follows (page 394):
3. Which lines need to be changed to make the code compile? (Choose all that apply.)


A. It compiles and runs without issue.
B. Line w1
C. Line w2
D. Line w3
E. Line w4
F. It compiles but throws an exception at runtime.

The answer is as follows (page 562):
3. B, C. The code does not compile, so A and F are incorrect. The first problem is that although a ScheduledExecutorService is created, it is assigned to an ExecutorService. Since scheduleWithFixedDelay() does not exist in ExecutorService, line w1 will not compile, and B is correct. The second problem is that scheduleWithFixedDelay()supports only Runnable, not Callable, and any attempt to return a value is invalid in a Runnable lambda expression; therefore line w2 will also not compile and C is correct. The rest of the lines compile without issue, so D and E are incorrect.

My question is why answer E is not correct. Line 4 does not compile because it requires the checked exceptions java.lang.InterruptedException and java.util.concurrent.ExecutionException to be caught or thrown.

Can someone explain me why answer E is not correct?

Regards,

Bart
 
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know, you can pass absolutely anything to System.out.println(...); it is overloaded to take all eight primitives and any reference type, real or null, so you can be sure line w4 will compile.
 
Bart Boersma
Ranch Hand
Posts: 72
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so although the exceptions java.lang.InterruptedException and java.util.concurrent.ExecutionException are not caught or thrown the code still compiles?

Because this is what I am seeing in IntelliJ (see attachment).
Schermafbeelding-2018-12-29-om-13.59.07.png
[Thumbnail for Schermafbeelding-2018-12-29-om-13.59.07.png]
 
Campbell Ritchie
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't include the method header, nor the try‑catch in the question. Those are lines outwith the scope of the question. The question only applies to code within the block shown.
 
Bart Boersma
Ranch Hand
Posts: 72
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I understand, but from question's code block no try-catch block nor a throw InterruptedException, ExecutionException is presented. That makes this code block not compile or at least we cannot conclude it will compile since we are not presented a way to deal with the checked exceptions.
 
author & internet detective
Posts: 39076
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the exam (and in the book), you are supposed to assume all necessary surrounding code is present.

Since there is no method declaration, you need to assume that it is correct. Which in this case means it throws an Exception.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!