File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes Marcus Green #3 Q19 (Thread question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Reply locked New topic

Marcus Green #3 Q19 (Thread question)

Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
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();
public void piggy(String sName){
sName = sName + " wiggy";
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"
Why is the answer POSSIBLE output of "vandeleur 0 1 2 3" ? I was sure that this was ALWAYS in the
output, because the thread is NOT a daemon thread...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The program has no synchronization, therefore you cannot predict whether run() will run before or after the System.out.println(sName); at the end of main().
It's even possible (though less likely) that you could get one of the following outputs:
vandeleur 0 1 2
vandeleur 0 1
vandeleur 0
So I don't think this question is in error.
[ August 20, 2002: Message edited by: Ron Newman ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Please do not post the same question in multiple forums.
Continue this discussion at

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: Marcus Green #3 Q19 (Thread question)
It's not a secret anymore!