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 Doubt in Marcus's question 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 "Doubt in Marcus Watch "Doubt in Marcus New topic
Author

Doubt in Marcus's question

Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
One of Marcus's exam questions -
What will happen 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 output of either "vandaleur", "vandaleur 0", "vandaleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"
The answer is 4. My doubt is, why doesnt the string "wiggy" get added? When we invoke the piggy() method, the 1st step is to add "wiggy", only then start the thread, but that doesnt appear in the output. Please explain.


Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Karthik,
sName in the method is local variable .. Thats the goof


SCJP 1.4, SCWCD 1.4
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
<IDIOT>i cant get more silly! </IDIOT>
This topic can be closed.
[ April 08, 2003: Message edited by: Karthik Veeramani ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you really want, because you started it, you can delete the first post and poof!!! All gone.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Good solution Barry .
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
To get an idea of how other people have fared on this question here are some statistics.
This count of attempts at this question is 1131
The count of times the attempt was correct is 454
A Rate of 40% correct responses.
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Marcus's question