Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

OCP 8 Study Guide chapter 7 question 21  RSS feed

 
Greenhorn
Posts: 4
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...Finally, any exception thrown in performCount() will appear as an exception
in the get() operation."
Method performCount does't have any checked exception in throws section.
So if we try to throw any checked exception in body of method, application would't compile.
Correct me if i'm wrong.

And sorry for my English. Thank you.

 
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please supply more details about the question, because many of us don't have that book, and also the book's authors and page number for those who do have the book.
I don't know, but I think it likely the authors were thinking about unchecked exceptions.
 
Ruslan Rakhmedov
Greenhorn
Posts: 4
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book what i'm talking about is "Oracle Certified Professional Java SE 8 Programmer II " by Jeanne Boyarsky and Scott Selikoff ISBN: 978-1-119-06790-0
And yes, i'm already think about unchecked exceptions.
But then, anwser to the question 21 in "Answer appendix on Chapter 7: Concurrency" need specify that it means unchecked exceptions
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the book's details. I don't have a copy however, so I don't know what the question actually says.
 
Ruslan Rakhmedov
Greenhorn
Posts: 4
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
21. Assuming an implementation of the performCount() method is provided prior to runtime,
which of the following are possible results of executing the following application? (Choose
all that apply.)

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;

public class CountZooAnimals
{
public static Integer performCount(int exhibitNumber)
{
// IMPLEMENTATION OMITTED
}

public static void printResults(Future<?> f)
{
try
{
System.out.println(f.get()); // o1
}
catch (Exception e)
{
System.out.println("Exception!");
}
}

public static void main(String[] args) throws InterruptedException,
ExecutionException
{
ExecutorService service = Executors.newSingleThreadExecutor();
final List<Future<?>> results = new ArrayList<>();
IntStream.range(0, 10)
.forEach(i -> results.add(
service.submit(() -> performCount(i)))); // o2
results.stream().forEach(f -> printResults(f));
service.shutdown();
}
}

A. It outputs a number 10 times.
B. It outputs a Boolean value 10 times.
C. It outputs a null value 10 times.
D. It outputs Exception! 10 times.
E. It hangs indefinitely at runtime.
F. It throws an unhandled exception at runtime.
G. The code will not compile because of line o1.
H. The code will not compile because of line o2.

and answers

21. A, C, D, E. The code compiles and runs without issue, so G and H are incorrect. The return type of performCount() is Integer, so the submit() is interpreted as being applied to a
Callable<Integer> value. In this manner, the Future<?> is really a Future<Integer> object. One possible implementation of performCount() is just to return the input parameter;
therefore A is a correct answer. B is incorrect, because the return type is Integer, not Boolean. The performCount() method could just return null, so C is a correct choice.
The performCount() can also throw a runtime exception; therefore D is also a correct answer. It is also possible for our performCount() to hang indefinitely, such as in a deadlock.
This would cause Future.get() to hang in printResults(), making E also a correct answer. Finally, any exception thrown in performCount() will appear as an exception in the get() operation.
Since the get() operations are caught in a try/catch block inprintResults(), none of them will be unhandled, and F is incorrect.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the book says, any Exceptions are caught by the printResults() method, so you won't get an unhandled Exception.

And thank you for providing all those details
 
Ruslan Rakhmedov
Greenhorn
Posts: 4
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. But i still think that performCount() can't throw any exception, it can throw only unchecked one.
 
Saloon Keeper
Posts: 3250
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ruslan,

correct. But a RuntimeEception is an Exception, so when an RTE is thrown, it is caught just as well. As an example of what the book means:
 
You can't expect to wield supreme executive power just because
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!