Wednesday 27 February 2013

21. What will be the output on compiling/running the following code?

21. What will be the output on compiling/running the following code?



Choices:
a. Compilation Error 
b. Prints : Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes and so on.
c. Prints : No No No No No No No No No No and so on.
d. Prints : Yes No Yes No Yes No Yes No Yes No and so on.
e. The Output cannot be determined.


Answer:-
E is correct.
Please note that there will not be any compilation error when the above code is compiled.
Also note that calling start() method on a Thread doesn't start the Thread.
It only makes a Thread ready to be called.
Depending on the operation system and other running threads, the thread on which start is called will get executed.
In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed.
Thus the output cannot be determined.

No comments:

Post a Comment