Which letters will eventually appear somewhere in the output (Choose all that apply.)

A. A

B. B

C. C

D. D

E. E

F. F

G. The answer cannot be reliably determined

H. The code does not compile

13. Given:

Image

 

And given the following five fragments:

Image

 

When the five fragments are inserted, one at a time at line 9, which are true (Choose all that apply.)

A. All five will compile

B. Only one might produce the output 4 4

C. Only one might produce the output 4 2

D. Exactly two might produce the output 4 4

E. Exactly two might produce the output 4 2

F. Exactly three might produce the output 4 4

G. Exactly three might produce the output 4 2

14. Given:

Image

 

Which are true (Choose all that apply.)

A. Compilation fails

B. The output could be r1 r2 m1 m2

C. The output could be m1 m2 r1 r2

D. The output could be m1 r1 r2 m2

E. The output could be m1 r1 m2 r2

F. An exception is thrown at runtime

15. Given:

Image

 

And given these two fragments:

Image

 

When fragment I or fragment II is inserted at line 5, which are true (Choose all that apply.)

A. An exception is thrown at runtime

B. With fragment I, compilation fails

C. With fragment II, compilation fails

D. With fragment I, the output could be yo dude dude yo

E. With fragment I, the output could be dude dude yo yo

F. With fragment II, the output could be yo dude dude yo

16. Given:

Image

 

Which are true (Choose all that apply.)

A. Compilation fails

B. The output could be 4 4 2 3

C. The output could be 4 4 2 2

D. The output could be 4 4 4 2

E. The output could be 2 2 4 4

F. An exception is thrown at runtime

17. Given:

Image

 

And given these two fragments:

Image

 

When either fragment I or fragment II is inserted at line 7, which are true (Choose all that apply.)

A. Compilation fails

B. With fragment I, an exception is thrown

C. With fragment I, the output could be 4 2 4 2

D. With fragment I, the output could be 4 4 2 3

E. With fragment II, the output could be 2 4 2 4

SELF TEST ANSWERS

1. The following block of code creates a Thread using a Runnable target:

Image

 

Which of the following classes can be used to create the target, so that the preceding code compiles correctly

A. public class MyRunnable extends Runnable{public void run(){}}

B. public class MyRunnable extends Object{public void run(){}}

C. public class MyRunnable implements Runnable{public void run(){}}

D. public class MyRunnable implements Runnable{void run(){}}

E. public class MyRunnable implements Runnable{public void start(){}}

Answer:

 

Image C is correct. The class implements the Runnable interface with a legal run() method.

 

Image 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(). (Objective 4.1)

 

2. Given:

Image

 

What is the result of this code

A. Compilation fails

B. 1..2..3..

C. 0..1..2..3..

D. 0..1..2..

E. An exception occurs at runtime

Answer:

 

Image Dis correct. The thread MyThread will start and loop three times (from 0 to 2).

 

Image A is incorrect because the Thread class implements the Runnable interface; therefore, in line 5, 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. (Objective 4.1)

 

Image

 

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

A. Each String in the array lines will print, with exactly a 1-second pause between lines

B. Each String in the array lines will print, with no pause in between because this method is not executed in a Thread

C. Each String in the array lines will print, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread

D. This code will not compile

E. Each String in the lines array will print, with at least a one-second pause between lines

Answer:

 

Image D is correct. The sleep() method must be enclosed in a try/catch block, or the method printAll() must declare it throws the InterruptedException.

 

Image 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. (Objective 4.2)

 

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.)

A. public int read(){return a+b;}
public void set(int a, int b){this.a=a;this.b=b;}

B. public synchronized int read(){return a+b;}
public synchronized void set(int a, int b){this.a=a;this.b=b;}

C. public int read(){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}

D. public int read(){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(b){this.a=a;this.b=b;}}

E. public synchronized(this) int read(){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}

F. public int read(){synchronized(this){return a+b;}}
public void set(int a, int b){synchronized(this){this.a=a;this.b=b;}}

Answer:

 

Image B and Fare correct. 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.

 

Image A is incorrect because it is not synchronized; therefore, there is no guarantee that the values added by the ead() method belong to the same pair. C and D are incorrect; only objects can be used to synchronize on. E fails—it is not possible to select other objects (even this) to synchronize on when declaring a method as synchronized. (Objective 4.3) Objective

 

5. Given:

Image

 

What is the result of trying to compile and run this program

A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 7

B. 1 2 3

C. 1 3

D. 1 2

E. At runtime, it throws an IllegalMonitorStateException when trying to wait

F. It will fail to compile because it has to be synchronized on the this object

Answer:

 

Image D is correct. 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.

 

Image 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. (Objective 4.4)

 

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 the thread A become a candidate to get another turn at the CPU

A. After object B is notified, or after two seconds

B. After the lock on B is released, or after two seconds

C. Two seconds after object B is notified

D. Two seconds after lock B is released

Answer:

 

Image A is correct. Either of the two events will make the thread a candidate for running again.

 

Image 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. (Objective 4.4)

 

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

A. The notifyAll() method must be called from a synchronized context

B. To call wait(), an object must own the lock on the thread

C. The notify() method is defined in class java.lang.Thread

D. When a thread is waiting as a result of wait(), it releases its lock

E. The notify() method causes a thread to immediately release its lock

F. The difference between notify() and notifyAll() is that notifyAll() notifies all waiting threads, regardless of the object they're waiting on

Answer:

 

Image A is correct because notifyAll() (and wait() and notify()) must be called from within a synchronized context. D is a correct statement.

 

Image 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 wrong because notify() is defined in java.lang.Object. E is wrong 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 wrong because notifyAll() notifies all the threads waiting on a particular locked object, not all threads waiting on any object. (Objective 4.4)

 

8. Given the 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:

Image

Image

 

How can we ensure that instances of this class can be safely used by multiple threads

A. This class is already thread-safe

B. Replacing StringBuilder with StringBuffer will make this class thread-safe

C. Synchronize the log() method only

D. Synchronize the getContents() method only

E. Synchronize both log() and getContents()

F. This class cannot be made thread-safe

Answer:

 

Image E is correct. Synchronizing the public methods is sufficient to make this safe, so F is false. This class is not thread-safe unless some sort of synchronization protects the changing data.

 

Image B is not correct because although a StringBuffer is synchonized internally, we call append() multiple times, and nothing would prevent two simultaneous log() calls from mixing up their messages. C and D are not correct 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 getString() on the StringBuffer as it's resizing its internal character array.) (Objective 4.3)

 

9. Given:

Image

 

What is the result of this code

A. It prints X and exits

B. It prints X and never exits

C. It prints XY and exits almost immeditately

D. It prints XY with a 10-second delay between X and Y

E. It prints XY with a 10000-second delay between X and Y

F. The code does not compile

G. An exception is thrown at runtime

Answer:

 

Image G is correct. 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 the answer would have been D.

 

Image A, B, C, D, E, and F are incorrect based the logic described above. (Objective 4.2)

 

10. Given:

Image

 

What is the result

A. foo

B. MyThread foo

C. MyThread bar

D. foo bar

E. foo bar baz

F. bar foo

G. Compilation fails

H. An exception is thrown at runtime

Answer:

 

Image B is correct. 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.

 

Image A, C, D, E, F, G, and H are incorrect based on the logic described above. (Objective 4.1)

 

11. Given:

Image

 

Which of the following pairs of method invocations could NEVER be executing at the same time (Choose all that apply.)

A. x.a() in thread1, and x.a() in thread2

B. x.a() in thread1, and x.b() in thread2

C. x.a() in thread1, and y.a() in thread2

D. x.a() in thread1, and y.b() in thread2

E. x.b() in thread1, and x.a() in thread2

F. x.b() in thread1, and x.b() in thread2

G. x.b() in thread1, and y.a() in thread2

H. x.b() in thread1, and y.b() in thread2

Answer:

 

Image A, F, and H. A is a right answer 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.)

 

Image C could happen because synchronized instance methods called on different instances do not block each other. B, D, E, and G could all happen because instance methods and static methods lock on different objects, and do not block each other. (Objective 4.3)

 

12. Given:

Image

 

Which letters will eventually appear somewhere in the output (Choose all that apply.)

A. A

B. B

C. C

D. D

E. E

F. F

G. The answer cannot be reliably determined

H. The code does not compile

Answer:

 

Image A, C, D, E, and F are correct. 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 1-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.

 

Image B, G, and H are incorrect based on the above. (Objective 4.4)

 

13. Given:

Image

 

And given the following five fragments:

Image

 

When the five fragments are inserted, one at a time at line 9, which are true (Choose all that apply.)

A. All five will compile

B. Only one might produce the output 4 4

C. Only one might produce the output 4 2

D. Exactly two might produce the output 4 4

E. Exactly two might produce the output 4 2

F. Exactly three might produce the output 4 4

G. Exactly three might produce the output 4 2

Answer:

 

Image C and D are correct. 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.

 

Image A, B, E, F, and G are incorrect based on the above. (Objective 4.1)

 

14. Given:

Image

 

Which are true (Choose all that apply.)

A. Compilation fails

B. The output could be r1 r2 m1 m2

C. The output could be m1 m2 r1 r2

D. The output could be m1 r1 r2 m2

E. The output could be m1 r1 m2 r2

F. An exception is thrown at runtime

Answer:

 

Image A is correct. 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.

 

Image B, C, D, E, and F are incorrect based on the above. (Objective 4.2)

 

15. Given:

Image

 

And given these two fragments:

Image

 

When fragment I or fragment II is inserted at line 5, which are true (Choose all that apply.)

A. An exception is thrown at runtime

B. With fragment I, compilation fails

C. With fragment II, compilation fails

D. With fragment I, the output could be yo dude dude yo

E. With fragment I, the output could be dude dude yo yo

F. With fragment II, the output could be yo dude dude yo

Answer:

 

Image F is correct. 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.

 

Image A, B, C, D, and E are incorrect based on the above. (Objective 4.3)

 

16. Given:

Image

 

Which are true (Choose all that apply.)

A. Compilation fails

B. The output could be 4 4 2 3

C. The output could be 4 4 2 2

D. The output could be 4 4 4 2

E. The output could be 2 2 4 4

F. An exception is thrown at runtime

Answer:

 

Image F is correct. 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.

 

Image A, B, C, D, and E are incorrect based on the above. (Objective 4.3)

 

17. Given:

Image

 

And given these two fragments:

Image

 

When either fragment I or fragment II is inserted at line 7, which are true (Choose all that apply.)

A. Compilation fails

B. With fragment I, an exception is thrown

C. With fragment I, the output could be 4 2 4 2

D. With fragment I, the output could be 4 4 2 3

E. With fragment II, the output could be 2 4 2 4

Answer:

 

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

 

Image A, B, and D are incorrect based on the above. (Objective 4.3)

 

EXERCISE ANSWERS

EXERCISE 9-1: Creating a Thread and Putting It to Sleep

The final code should look something like this:

Image

 

EXERCISE 9-2: Synchronizing a Block of Code

Your code might look something like this when completed:

Image

 

Just for fun, try removing lines 1 and 2 then run the program again. It will be unsynchronized—watch what happens.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset