This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This is a question from CertPal.
In the following program isn't the answer - NONE OF THE ABOVE correct.
" Yes" will be printed at last due to execution of join() but which one among "0 1 2 3 4"
and "Hello" is printed is not guaranteed.Just confirming the answer.
I think the answer should be hello 01234 yes.
because if after starting the test thread if main gets control it will print hello and then join the test thread but if test thread gets the control it goes to sleep state for 2 seconds due to which main thread will take control and will join the test thread after printing hello.
either way hello is printed first then test threads for loop executes and prints 01234 and then after its completion main thread will print yes.
Please tell me if I am wrong. This is really bad feeling about getting confused after passing the exam.
Neha, when the Test thread sleeps for 2 seconds, there are no guarantees that the main thread will get a chance to execute and print Hello. So the output cannot be guaranteed to be "hello 01234 yes"...
I agree that a decent thread scheduler should give the output that you suggest. Perhaps the timeout there is a little too exaggerated. However in theory the output can still not be guaranteed, which is what the SCJP exam would expect from you.
I will update the timeout when time allows
Joined: Oct 30, 2009
ok....... thanks Ankit and Deepak.
Joined: Jan 09, 2010
Deepak , is the link (SCJP 5 Mock exam (Word document ) ) from Inquisition