Elite Quiz 19
Elite Quiz 19
Question 1
Which of the following statements about the Callable call() and Runnable run()
Correct
methods are correct? (Choose all that apply.)
Mark 1.00 out of
1.00 Select one or more:
a. Both can throw unchecked exceptions.
A, C, D, F. Runnable and Callable statements both take no method arguments as input, so B is incorrect. Runnable returns
void and Callable returns a generic type, so F is correct, and E and G are incorrect. All methods are capable of throwing
unchecked exceptions, so A is correct. Only Callable is capable of throwing checked exceptions, so C is correct. Both
Runnable and Callable can be implemented with lambda expressions, so D is correct.
Question 2
Which of these statements compile? (Choose all that apply.)
Correct
B, C, F. Option A does not compile because the generic types are not compatible. We could say HashSet hs2 = new
HashSet();. Option B uses a lower bound, so it allows superclass generic types. Option C is a traditional use of generics
where the generic type is the same and the List type uses the interface as the type. Option D does not compile because a
Set is not a List. Option E does not compile because upper bounds are not allowed when instantiating the type. Finally,
Option F does compile because the upper bound is on the correct side of =.
b. The task will be added to an internal queue and completed when there is an available
thread.
B. If a task is submitted to a thread executor, and the thread executor does not have any available threads, the call to the
task will return immediately with the task being queued internally by the thread executor. For this reason, B is the only
correct answer.
c. The thread executor will discard any task over its thread limit.
d. The call to submit the task to the thread executor will wait until there is a thread avail-
able before continuing.
e. The thread executor creates new temporary threads to complete the additional tasks.
Question 4
Correct
b. onetwo7
E. The code does not compile. It attempts to mix generics and legacy code. Lines 3 through 7 create an ArrayList without
generics. This means that we can put any objects in it. Line 7 should be looping through a list of Objects rather than Strings
since we didn’t use generics.
A. The code compiles without issue, so D is incorrect. The CopyOnWriteArrrayList class is designed to preserve the
original list on iteration, so the first loop will be executed exactly three times and E is incorrect. The ConcurrentSkipListSet
class allows modifications while iterating, so it is possible that the second loop could generate an infinite loop. In this case,
though, the second loop executes exactly four times, since elements in a set are unique and 5 can be added only once. For
these reasons, F and G are also incorrect. Finally, despite using the elements of l1 to populate the collections, l2 and s3
are not backed by the original list, so the size of l1 is 3. Likewise, the size of l2 is 6 and the size of s3 is 4, so A is the
correct answer.
b. It outputs 6 6 6.
c. It outputs 6 3 4.
Question 6
Which of the following properties of concurrency are true? (Choose all that apply.)
Correct
d. Applications with many resource-heavy tasks tend to benefit more from concurrency
than ones with CPU-intensive tasks.
A, D. By itself, concurrency does not guarantee which task will be completed first, so A is correct. Furthermore, applications
with numerous resource requests will often be stuck waiting for a resource, which allows other tasks to run. Therefore, they
tend to benefit more from concurrency than CPU-intensive tasks, so D is also correct. B is incorrect because concurrency
may in fact make an application slower if it is truly single-threaded in nature. Keep in mind that there is a cost associated
with allocating additional memory and CPU time to manage the concurrent process. C is incorrect because single-
processor CPUs have been benefiting from concurrency for decades. Finally, E is incorrect; there are numerous examples
in this chapter of concurrent tasks sharing memory.
b. hellohi
c. hellohiola
d. hi
e. hihello
E. Since we call push() rather than offer(), we are treating the ArrayDeque as a LIFO (last-in, first-out) stack. On line 7, we
remove the last element added, which is "ola". On line 8, we look at the new last element ("hi"), but don’t remove it. Lines 9
and 10, we remove each element in turn until none are left. Note that we don’t use an Iterator to loop through the
ArrayDeque. The order in which the elements are stored internally is not part of the API contract.
g. An exception is thrown.
E. The program compiles without issue, so B, C, and D are incorrect. Lines m2 and m3 throw a compiler warning about
generics but still compile. Notice that RecursiveAction, unlike RecursiveTask, does not return a value. However, since we
used a generic ForkJoinTask reference, the code still compiles. The issue here is that the base condition is not reached
since the numbers start/end are consistently positive. This causes an infinite loop, although since memory is finite, Java
detects this and throws a StackOverflowError, so E is correct. In practice, this could also generate a locking exception
before the StackOverflowError when the program runs out of memory, but in either circumstance, the program will exit.
Correct
F, H. The application compiles and does not throw an exception, so B, C, D, E, and G are incorrect. Even though the
stream is processed in sequential order, the tasks are submitted to a thread executor, which may complete the tasks in any
order. Therefore, the output cannot be determined ahead of time and F is correct, making A incorrect. Finally, the thread
executor is never shut down; therefore the code will run but it will never terminate, making H also correct
Question 10
Correct
b. one
c. One
C. TreeSet sorts the elements. Since uppercase letters sort before lowercase letters, the ordering is "ONE", "One", "one".
The ceiling() method returns the smallest element greater than the specified one. "On" appears between "ONE" and "One".
Therefore, the smallest element that is larger than the specified value is "One".
d. ONE
f. An exception is thrown.
Correct
B. The code compiles and runs without issue, so D, E, F, and G are incorrect. The key aspect to notice in the code is that a
single-thread executor is used, meaning that no task will be executed concurrently. Therefore, the results are valid and
predictable with 100 100 being the output, and B is the correct answer. If a pooled thread executor was used with at least
two threads, then the sheepCount2++ operations could overwrite each other, making the second value indeterminate at the
end of the program. In this case, C would be the correct answer.
Correct
e. The class correctly prevents concurrency issues for the value of tickets when accessed
by multiple threads.
A, F. The class compiles without issue so A is correct, and B and C are incorrect. The synchronized object on line k1 is
TicketManager.class, while the synchronized object on line k4 is the instance of TicketManager. The class is not thread-
safe because the makeTicketsAvailable() method is not synchronized, and E is incorrect. One thread could call sellTickets()
while another thread has unblocked accessed to makeTicketsAvailable(), causing an invalid number of tickets to be
reached as part of a race condition. Finally, F is correct because the class synchronizes using a static getInstance()
method, preventing more than one instance from being created.
Correct
b. It prints 1 2 3 4 5
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.
Question 14
Correct
b. Deadlock, Starvation
D. Livelock occurs when two or more threads are conceptually blocked forever, although they are each still active and
trying to complete their task. A race condition is an undesirable result that occurs when two tasks are completed at the
same time, which should have been completed sequentially. For these reasons, D is the only correct answer.
f. Deadlock, Livelock
b. ArrayList
B. The answer needs to implement List because the scenario allows duplicates. Since you need a List, you can eliminate
C, D, and E immediately. HashMap is a Map and HashSet is a Set. LinkedList is both a List and a Queue. You want a
regular List. Option A, Arrays, is trying to distract you. It is a utility class rather than a Collection. An array is not a
collection. By process of elimination, the answer is B.
c. HashMap
d. HashSet
e. LinkedList