• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mind Q Answer Issue..

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

myThread.start()

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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
2.now,m=1 which is again less than 2 .then m is incremented.
so 2 is printed.
3.now m=2 and condition is not satisfied.so 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...
vinu.
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!.

Regards,
Priya.
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


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

Cheers
 
nagaraju parepally
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
i am thinking yield() wont give 100% guarentee to stop the current thread
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it is not 100% guaranteed, that's why question was framed with word 'might'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic