Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Marcus's question

 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Reshma Shanbhag
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik,
sName in the method is local variable .. Thats the goof
 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<IDIOT>i cant get more silly! </IDIOT>
This topic can be closed.
[ April 08, 2003: Message edited by: Karthik Veeramani ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want, because you started it, you can delete the first post and poof!!! All gone.
 
Reshma Shanbhag
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good solution Barry .
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic