PASS - TAKER : Anonymos

Java SE 8 OCP - Concurrency

Correct : 13

68% Complete (success)

68 %

False : 6

31% Complete (success)

31 %



Anonymos 2019-10-20T18:24:43

Correct

1. The following block of code creates a CopyOnWriteArrayList, adds elements to it, and prints the contents: {code} What is the result?

The Iterator is obtained before 6 is added. As long as the reference to the Iterator is maintained, it will only provide access to the values 4 and 2.

Correct

2. Given: {code} Which shows the output that will be produced?

Because the Iterator is obtained before the number 2 is removed, it will reflect all the elements that have been added to the collection.

Correct

3. Which methods from a CopyOnWriteArrayList will cause a new copy of the internal array to be created? (Choose all that apply.)

Of the methods listed, only add and remove will modify the list and cause a new internal array to be created.

Not Correct

4. Given: {code} Which operation(s) can block indefinitely? (Choose all that apply.)

The add method will throw an IllegalStateException if the queue is full. The two offer methods will return false if the queue is full. Only the put method will block until space becomes available.

Not Correct

5. Given the following code fragment: {code} What do you expect the output to be, and which thread(s) will be responsible for the output? (Choose all that apply.)

you could see the output in either order because we don’t know in advance if threads t1, t2, and t3 will complete before the main thread or if the main thread will complete first. However, in either case, t1, t2, and t3 will wait at the barrier until t1, t2, and t3 are all at the barrier, and the last thread to reach the barrier will display the output. A, B, D, and E are incorrect; A and B are incorrect because of the above. D is incorrect because the Singleton is thread-safe; enum singletons are guaranteed to be created in a thread-safe manner, and we have synchronized the updateValue() method. E is incorrect because it is highly unlikely one of these threads will get stuck or time out and none is accessing any collection that is not thread-safe.

Not Correct

6. Given: {code} Which method(s) would delete John from the map only if his value was still equal to 23?

it uses the correct syntax. The methods for answers A, B, and D do not exist in a ConcurrentHashMap. A traditional Map contains a single-argument remove method that removes an element based on its key. The ConcurrentMap interface (which ConcurrentHashMap implements) added the two-argument remove method, which takes a key and a value. An element will only be removed from the Map if its value matches the second argument. A boolean is returned to indicate if the element was removed.

Correct

7. Which method represents the best approach to generating a random number between one and ten if the method will be called concurrently and repeatedly by multiple threads?

The ThreadLocalRandom creates and retrieves Random instances that are specific to a thread. You could achieve the same effect prior to Java 7 by using the java.lang.ThreadLocal and java.util.Random classes, but it would require several lines of code. Math.random is thread-safe, but uses a shared java.util.Random instance and can suffer from contention problems.

Correct

8. Given: {code} Which atomically increment i by 9? (Choose all that apply.)

The addAndGet and getAndAdd both increment the value stored in an AtomicInteger. Answer C is not atomic because in between the call to get and set, the value stored by i may have changed. Answer D is invalid because the atomicIncrement method is fictional, and answer E is invalid because auto-boxing is not supported for the atomic classes. The difference between the addAndGet and getAndAdd methods is that the first is a prefix method (++x) and the second is a postfix method (x++).

Not Correct

9. Given: {code} Which block(s) of code best match the behavior of the methods in the LeaderBoard class? (Choose all that apply.)

The addScore method modifies the collection and, therefore, should use a write lock, while the getHighScores method only reads the collection and should use a read lock.

Not Correct

10. Given: {code} What is the result?

A lock counts the number of times it has been locked. Calling lock increments the count, and calling unlock decrements the count. If a call to unlock decreases the count below zero, an exception is thrown.

Correct

11. Which class contains factory methods to produce preconfigured ExecutorService instances?

Executor is the super-interface for ExecutorService. You use Executors to easily obtain ExecutorService instances with predefined threading behavior. If the Executor interface does not produce ExecutorService instances with the behaviors that you desire, you can always look into using java.util.concurrent.AbstractExecutorService or java.util.concurrent.ThreadPoolExecutor directly.

Not Correct

12. Given: {code} Which set(s) of lines, when inserted, would correctly use the ExecutorService argument to execute the Callable and return the Callable's result? (Choose all that apply.)

When you submit a Callable to an ExecutorService for execution, you will receive a Future as the result. You can use the Future to check on the status of the Callable's execution, or just use the get method to block until the result is available.

Correct

13. Which are true? (Choose all that apply.)

When you submit a Callable to an ExecutorService for execution, you will receive a Future as the result. You can use the Future to check on the status of the Callable's execution, or just use the get method to block until the result is available.

Correct

14. Given: {code} Which line(s), when inserted at the end of the compute method, would correctly take the place of separate calls to fork() and join()? (Choose all that apply.)

The invokeAll method is a var args method that will fork all Fork-Join tasks, except one that will be invoked directly.

Correct

15. When writing a RecursiveTask subclass, which are true? (Choose all that apply.)

When creating multiple ForkJoinTask instances, all tasks except one should be forked first so that they can be picked up by other Fork-Join worker threads. The final task should then be executed within the same thread (typically by calling compute()) before calling join on all the forked tasks to await their results. In many cases, calling the methods in the wrong order will not result in any compiler errors, so care must be taken to call the methods in the correct order.

Correct

16. Given the following code fragment: {code} What is the result?

The result is unpredictable because the reduction function is not associative and the stream is parallel.

Correct

17. Given the following code fragment: {code} What is the result?

Because of the limit(2), we will see the dog names twice, most likely in order. The limit() forces the stream to maintain state about the source data, so it is likely the stream will retain the original source ordering (although that is not guaranteed) even though it’s an unordered parallel stream.

Correct

18. Given the following code fragment: {code} What is the result?

Unlike the previous sample question, we have no limit() so we have a stateless stream pipeline. The stream is unordered, and the pipeline is executing in parallel, so we cannot predict the ordering of the output. Note that if we were using forEachOrdered() instead of forEach() as the terminal operation, it is likely, but not guaranteed, that we would see the results in the same ordering as the source.

Correct

19. Given the code fragment: {code} What is the result?

For a parallel stream, any of the numbers can be multiplied by 2 and returned by findAny().