wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Threads 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 "Question on Threads " Watch "Question on Threads " New topic
Author

Question on Threads

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Kb Book THreads Self-test pg-782






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.)
A. All five will compile
B. Only one might produce the output 4 4
C. Only one might produce the output 4 2
D. Exactly two might produce the output 4 4
E. Exactly two might produce the output 4 2
F. Exactly three might produce the output 4 4
G. Exactly three might produce the output 4 2


Ans: C And D


i too think that C is correct but how's D possible.can anyone explain

only III,IV V would work when inserted ,but only V would result in any output.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For D, What about fragment I and IV?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

what do you mean to say Abimaran Kugathasan ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

mohitkumar gupta wrote:what do you mean to say Abimaran Kugathasan ?


He means... why don't you think I and IV will run, and produce any output?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:what do you mean to say Abimaran Kugathasan ?


The fragments I and IV will work, and the output may be 4 4. (We can't tell sure that Thread ID is 4, but for this question, it'll print same ID, because same thread will run in the code)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

And, Henry, could you please check your private message regarding a topic?

Thanks in Advanced!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

The fragments I and IV will work, and the output may be 4 4. (We can't tell sure that Thread ID is 4, but for this question, it'll print same ID, because same thread will run in the code)

That's why the answer says "Exactly two might produce the output 4 4"', It's the nature of the output not the exact value.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vijitha Kumara wrote:
That's why the answer says "Exactly two might produce the output 4 4"', It's the nature of the output not the exact value.


Yea, The answers C and D, that's why I told it earlier!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Threads
 
Similar Threads
Regarding getId
Question 13 chapter 9 of k&B
Question of SCJP Guide by Kathy Sierra - Chapter Threads
Creating and Starting a Thread Properly
Answering Strategy