PASS - TAKER : Anonymous

Java SE 8 OCP - Threads

Correct : 7

38% Complete (success)

38 %

False : 11

61% Complete (success)

61 %

Anonymous 2018-08-27T11:22:55


1. The following block of code creates a Thread using a Runnable target: {code} Which of the following classes can be used to create the target so that the preceding code compiles correctly?

The class implements the Runnable interface with a legal run() method. A is incorrect because interfaces are implemented, not extended. B is incorrect because even though the class has a valid public void run() method, it does not implement the Runnable interface. D is incorrect because the run() method must be public. E is incorrect because the method to implement is run(), not start().


2. Given: {code} What is the result of this code?

The thread MyThread will start and loop three times (from 0 to 2). A is incorrect because the Thread class implements the Runnable interface; therefore, in line 6, Thread can take an object of type Thread as an argument in the constructor (this is NOT recommended). B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2. E is incorrect based on the above.

Not Correct

3. Given: {code} The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?

The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the InterruptedException. E is incorrect, but it would be correct if the InterruptedException was dealt with (A is too precise). B is incorrect (even if the InterruptedException was dealt with) because all Java code, including the main() method, runs in threads. C is incorrect. The sleep() method is static; it always affects the currently executing thread.


4. Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.)

By marking the methods as synchronized, the threads will get the lock of the this object before proceeding. Only one thread will be setting or reading at any given moment, thereby assuring that read() always returns the addition of a valid pair. A is incorrect because it is not synchronized; therefore, there is no guarantee that the values added by the read() method belong to the same pair. C and D are incorrect; only objects can be used to synchronize on. E is incorrect because it fails—it is not possible to select other objects (even this) to synchronize on when declaring a method as synchronized.


5. Given: {code} What is the result of trying to compile and run this program?

1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. It's frozen at line 7. A is incorrect; IllegalMonitorStateException is an unchecked exception. B and C are incorrect; 3 will never be printed, since this program will wait forever. E is incorrect because IllegalMonitorStateException will never be thrown because the wait() is done on args within a block of code synchronized on args. F is incorrect because any object can be used to synchronize on, and this and static don't mix.

Not Correct

6. Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will thread A become a candidate to get another turn at the CPU?

Either of the two events will make the thread a candidate for running again. B is incorrect because a waiting thread will not return to runnable when the lock is released unless a notification occurs. C is incorrect because the thread will become a candidate immediately after notification. D is also incorrect because a thread will not come out of a waiting pool just because a lock has been released.

Not Correct

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

A is correct because notifyAll() (and wait() and notify()) must be called from within a synchronized context. D is a correct statement. B is incorrect because to call wait(), the thread must own the lock on the object that wait() is being invoked on, not the other way around. C is incorrect because notify() is defined in java.lang.Object. E is incorrect because notify() will not cause a thread to release its locks. The thread can only release its locks by exiting the synchronized code. F is incorrect because notifyAll() notifies all the threads waiting on a particular locked object, not all threads waiting on any object.

Not Correct

8. Given this scenario: This class is intended to allow users to write a series of messages so that each message is identified with a timestamp and the name of the thread that wrote the message: {code} How can we ensure that instances of this class can be safely used by multiple threads?

Synchronizing the public methods is sufficient to make this safe, so F is incorrect. This class is not thread-safe unless some sort of synchronization protects the changing data. B is incorrect because although a StringBuffer is synchronized internally, we call append() multiple times, and nothing would prevent two simultaneous log() calls from mixing up their messages. C and D are incorrect because if one method remains unsynchronized, it can run while the other is executing, which could result in reading the contents while one of the messages is incomplete, or worse. (You don't want to call toString() on the StringBuffer as it's resizing its internal character array.)


9. Given: {code} What is the result of this code?

The code does not acquire a lock on t before calling t.wait(), so it throws an IllegalMonitorStateException. The method is synchronized, but it's not synchronized on t so the exception will be thrown. If the wait were placed inside a synchronized(t) block, then D would be correct.

Not Correct

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

In the first line of main we're constructing an instance of an anonymous inner class extending from MyThread. So the MyThread constructor runs and prints MyThread. Next, main() invokes start() on the new thread instance, which causes the overridden run() method (the run() method in the anonymous inner class) to be invoked.

Not Correct

11. Given: {code} Which of the following pairs of method invocations could NEVER be executing at the same time? (Choose all that apply.)

A is correct because when synchronized instance methods are called on the same instance, they block each other. F and H can't happen because synchronized static methods in the same class block each other, regardless of which instance was used to call the methods. (An instance is not required to call static methods; only the class.) C, although incorrect, could happen because synchronized instance methods called on different instances do not block each other. B, D, E, and G are incorrect but also could all happen because instance methods and static methods lock on different objects, and do not block each other.

Not Correct

12. Given: {code} Which letters will eventually appear somewhere in the output? (Choose all that apply.)

This may look like laurel and hardy are battling to cause the other to sleep() or wait()—but that's not the case. Since sleep() is a static method, it affects the current thread, which is laurel (even though the method is invoked using a reference to hardy). That's misleading, but perfectly legal, and the Thread laurel is able to sleep with no exception, printing A and C (after at least a one-second delay). Meanwhile, hardy tries to call laurel.wait()—but hardy has not synchronized on laurel, so calling laurel.wait() immediately causes an IllegalMonitorStateException, and so hardy prints D, E, and F. Although the order of the output is somewhat indeterminate (we have no way of knowing whether A is printed before D, for example), it is guaranteed that A, C, D, E, and F will all be printed in some order, eventually—so G is incorrect.

Not Correct

13. Given: {code} And given the following five fragments: I. new Starter().run(); II. new Starter().start(); III. new Thread(new Starter()); IV. new Thread(new Starter()).run(); V. new Thread(new Starter()).start(); When the five fragments are inserted, one at a time at line 9, which are true? (Choose all that apply.)

Fragment I doesn't start a new thread. Fragment II doesn't compile. Fragment III creates a new thread but doesn't start it. Fragment IV creates a new thread and invokes run() directly, but it doesn't start the new thread. Fragment V creates and starts a new thread.

Not Correct

14. Given: {code} Which are true? (Choose all that apply.)

The join() must be placed in a try/catch block. If it were, answers B and D would be correct. The join() causes the main thread to pause and join the end of the other thread, meaning "m2" must come last.

Not Correct

15. Given: {code} And given these two fragments: I. synchronized void chat(long id) { II. void chat(long id) { When fragment I or fragment II is inserted at line 5, which are true? (Choose all that apply.)

With Fragment I, the chat method is synchronized, so the two threads can't swap back and forth. With either fragment, the first output must be yo.

Not Correct

16. Given: {code} Which are true? (Choose all that apply.)

When run() is invoked, it is with a new instance of ChicksYack and c has not been assigned to an object. If c were static, then because yack is synchronized, answers C and E would have been correct.


17. Given: {code} And given these two fragments: I. synchronized void move(long id) { II. void move(long id) { When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.)

E should be obvious. C is correct because even though move() is synchronized, it's being invoked on two different objects.


18. You have two threads, t1 and t2, attemping to access a shared resource, and t2 is always descheduled when it tries to access that resource. What is this kind of problem called?

Starvation occurs when one or more threads cannot get access to a resource.