Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Mock Exam Errata and the fly likes Marcus Green exam #1 question 19 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Marcus Green exam #1 question 19" Watch "Marcus Green exam #1 question 19" New topic
Author

Marcus Green exam #1 question 19

t capelle
Greenhorn

Joined: Apr 23, 2001
Posts: 3
The question states:
What will happend when you attempt to compile and run the following code?
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);

}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;

}
}
}
1) Compile time error
2) Compilation and output of "vandeleur wiggy"
3) Compilation and output of "vandeleur wiggy 0 1 2 3"
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
The answer says:
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.
----------------
How can this be? The thread was never started so the run method will not be called. 'piggy' will be called because it is directly called in the main method. I thought the answer would be 2. Can someone explain why I'm wrong? Thanks.
David Wake
Greenhorn

Joined: Apr 18, 2001
Posts: 17
The piggy() method includes a call to start()
David
David Wake
Greenhorn

Joined: Apr 18, 2001
Posts: 17
I missed your other questions. The reason "wiggy" does not get printed is that sName is passed by value to the piggy() method. When the line
sName = sName + " wiggy" ;
is executed, only the copy local to the piggy() method is altered.
The reason why we cannot be sure what the output will be is that things depend on the implementation of the threading mechanism. once the line
start()
is executed in the piggy() method, we have 2 threads: the main thread and the new one just started. There is now a race between the two threads: how much will the new thread do before the main thread executes
System.out.println(sName) ;
??
If it does nothing, then "vandeleur" alone will be printed. However, if it does manage to do something, then numbers may be appended to the output. Indeed, I believe that any of "vandeleur 0", "vandeleur 0 1", "vandeleur 0 1 2", or "vandeleur 0 1 2 3" are possible.
If the underlying threading model is pre-emptive then "vandeleur 0 1 2 3" will be printed. If the underlying threading model is time-sliced then in all probability "vandeleur" will be printed, but anything is possible.
Hope that helped.
David
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Excellent explanation David. Note that this question was previously rather flawed and was updated and corrected as a result of a post at Javaranch. Isn't the web (and the ranch) wonderful.
Marcus
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
madhuri vl
Ranch Hand

Joined: Jan 17, 2001
Posts: 86
Hi,
From which mockexam u got this Question? I just want to go thru...
Madhuri.


madhuri.
madhuri vl
Ranch Hand

Joined: Jan 17, 2001
Posts: 86
Hi
Sorry. I did not notice it is clearly stated from marcus green #1.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
piggy? wiggy?
Can you tell Marcus is from England?
[This message has been edited by Thomas Paul (edited April 26, 2001).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Would that be hoggy woggy in the US version of English?
See flag at http://www.jchg.net
Ugh the g from pig got into my url and I had it as www.jchg <===
Marcus
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================
[This message has been edited by Marcus Green (edited May 04, 2001).]
Percy Densmore
Ranch Hand

Joined: Mar 06, 2001
Posts: 214
Marcus,
Your 'See flag line...' has a typo in the URL.
Regards,
Percy
PS I accidentally sent you an email to theWellsCafe, just disregard it. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green exam #1 question 19