File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mind Q Answer Issue.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mind Q Answer Issue.." Watch "Mind Q Answer Issue.." New topic

Mind Q Answer Issue..

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi ,
Here is a snapshot of code:

29. For the code:

m = 0;
while( m++ < 2 )
System.out.println( m );

Which of the following are printed to standard output?

a) 0
b) 1
c) 2
d) 3
e) Nothing and an exception is thrown

They say answer is b and c .
I feel its 0 and 1. Is variable m incremented before or after comparison since its postfix i felt its after..
Please clear this..

Another snapshot:

For what reasons might a thread stop execution?

a) A thread with higher priority began execution.
b) The thread's wait() method was invoked.
c) The thread invoked its yield() method.
d) The thread's pause() method was invoked.
e) The thread's sleep() method was invoked.

Answer given is a,b,c,e.

I feel its a,b,e..Does yield method stop the execution .I read in some site it only stops execution if higher priority thread is present else it might start running again..
Whats the exact concept here..

Third snapshot:
What line of code would begin execution of a thread named myThread?


was marked wrong since i didnt put semicolon.

Should we always put semicolon even if it is fill in blanks....

Please let me know..

Thanks in Advance...
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
For the first question.....m is incremented after the comparison.
let us go through the code.
1.m=0 which is less than 2.then m is incremented.
so 1 is printed.,m=1 which is again less than 2 .then m is incremented.
so 2 is printed. m=2 and condition is not the loop ends here.however m gets incremented.
Regarding yield method,you are right.
if there are no higher priority threads or threads of same priority in the runnable state,the same thread gets executed.
hope this helps...
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Smitha,

Hope ur first doubt is cleared with Vinu's explanation.

Reg 2nd snapshot :

Read the question carefully.It asks "For what reasons might a thread stop execution?".When yield method is invoked if at all higher priority thread exists than the current one then JVM scheduler may choose this higher priority thread for execution and stop(in other words pause) the execution of the current thread.Hope this is clear.

Reg 3rd snapshot :

Again question should be read carefully.They're asking for a line of code.So exact syntax would be expected!!.

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99

Thanks for the response.. My queries are cleared...

nagaraju parepally

Joined: Aug 04, 2005
Posts: 2
i am thinking yield() wont give 100% guarentee to stop the current thread
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
yes, it is not 100% guaranteed, that's why question was framed with word 'might'.
I agree. Here's the link:
subject: Mind Q Answer Issue..
It's not a secret anymore!