Java Reference
In-Depth Information
11. D. The code compiles fi ne, so E is incorrect. A thread cannot invoke wait on an object
unless that thread owns the object's monitor lock. In other words, a call to wait must
appear within a synchronized method or block of code. Line 11 of the MyConsumer class
generates an IllegalMonitorStateException at runtime because the thread does not own
the lock of sb. Therefore, the answer is D.
12. D. You cannot start a thread twice. Line 17 compiles, but the r thread has already been
started, so line 17 generates an IllegalThreadStateException. Therefore, the answer is D.
13. B. The state of t is NEW after it is instantiated on line 9, and t becomes
RUNNABLE once it is started. Therefore, the answer is B.
14. A. This question is tricky. The state of t is NEW after it is instantiated on line 7. Because t is
never started, it does not change states. The sleep method is static and causes the current
thread to sleep, not the thread t. Therefore, the answer is A.
15. C. The code compiles and runs fi ne, so A and B are incorrect. E is false; there is no such
requirement of the getFirstDay method. D is also false; owning the lock of a particular
Calendar object is not suffi cient for entering setFirstDay. The thread that enters
setFirstDay must own the lock of the Class object of Calendar. Therefore, the answer is C.
Search WWH ::




Custom Search