File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 13 chapter 9 of k&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question 13 chapter 9 of k&B" Watch "Question 13 chapter 9 of k&B" New topic
Author

Question 13 chapter 9 of k&B

Mauricio Archbold Babroza
Greenhorn

Joined: Apr 01, 2009
Posts: 11

And given the following five fragments:
I. new Starter().run();
II. new Starter().start();
III. new Thread(new Starter());
IV. new Thread(new Starter()).run();
V. new Thread(new Starter()).start();
When the five fragments are inserted, one at a time at line 9, which are true? (Choose all that apply.)
these are the answer
C. Only one might produce the output 4 2
D. Exactly two might produce the output 4 4
, but why??? if i run this code the output is 1 8 in my pc???


Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
You are on the right track. Your output agrees with answer 'C' because
there will be two outputs, one from main() and then a second from run().
The actual numbers can vary because the JVM assigns the ID numbers.
Here's the code with the four fragments that compile.
Jim ... ...


BEE MBA PMP SCJP-6
Greg Beresnev
Greenhorn

Joined: Jun 05, 2010
Posts: 2
I think the question I had about question 13 was... when I was doing the self test I answered B and C because I assume no two threads will have the same ID number. So is answer D made possible by the fact that the main thread might finish executing by the time the Starter thread's run method and the print statement in it is executed? Would have been nice if it was clarified, since the explanation of the correct answers just states 'Fragment V creates and starts a new thread'. Or maybe I'm just not very bright
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question 13 chapter 9 of k&B