This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
This is the answer : G. An exception is thrown at runtime And the book says why : 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.
Even if we make the above correction, is the below possible ? The run() method of t contains nothing, so it can probably complete in a nano-second. Assuming that the jvm decides to move it from runnable to running immediately after start() ,
it might just complete before main can print X. Then we are trying to call wait on a dead thread...could that cause an exception ? If there is some misconception , please point it out.