aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Mock- doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Mock- doubt" Watch "Marcus Green Mock- doubt" New topic
Author

Marcus Green Mock- doubt

sudhir harsha
Greenhorn

Joined: Jan 23, 2006
Posts: 19
Hi group,
Can any one please explain how the ans is 4,
I thought the ans is 1. Because I thought that we can call start() only thru a Thread object.

///Q.19
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 "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48

[ January 26, 2006: Message edited by: Brandon Tom ]

Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:tombrand@hawaii.edu" target="_blank" rel="nofollow">tombrand@hawaii.edu</a>
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
hi sudhir,

when calling t.piggy(), you actually call the start() method on a thread object (on t). instead of start(), it could have said this.start(), where this refers to t.

brandon, which result did you get? would be interesting! (i got one of the expected results)

greetings
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Sudir,

The output can`t be exaclty determined, the fourth answer is correct.



Sorry for a little confusing explanation but my english sucks.

It goes like this.... we start in main at line 3, then 4, 5, then there`s a method invocation so we go to 7, 8, 9... and here`s the place from which we don`t know where the program will go to. And a small note just in case, in the piggy method, static field of type String declared and initialized in Tux class will not change. Local variable sName is a copy of static field, since it has the same name as the static field declared in Tux class and there`s no explicit refrence this (even though it`s a static member) like "this.sName = sName + "wiggy"; in this method local variable sName shadows the static field and we are making operations on local variable all the time, it dies when the metod dies, static field remains the same.


OK, let`s go back to the main problem. Assume it ends invocation and goes back to main.

Now it can execute line 6 and print "vandeleur", then move to thread of execution t, and do the for loop, but there`s no another print statement to print static String sName variable, so the output is "vandeleur".

Or after invocation of t.piggy(sName) at line 5, it could move to thread of execution t, start for loop and for example after one iteration come back to main and print the static String sName variable which would be then "vandeleur 0" then move back to thread of execution t and finish the loop.... or it could be after two iterations, then "vandeleur 0 1" or after three iteration... etc.
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
In support to Aleksander, answer.

To get an output(rather force) other than what is printed in main method



Parallax - Change in observational position that provides a new line of sight.
Faqeer Khudaka
Ranch Hand

Joined: Jan 24, 2006
Posts: 32
Hi Aleksander,

Threads are a bit confusing for me.

OK, let`s go back to the main problem. Assume it ends invocation and goes back to main.

Now it can execute line 6 and print "vandeleur", then move to thread of execution t, and do the for loop, but there`s no another print statement to print static String sName variable, so the output is "vandeleur".


Could you please clarify how after the method invocation and after line 9 the for loop is not completely executed and why the output is not "vandeleur 0 1 2 3" only .
Second question, how can after thread t has started we move back and forth between the for loop and the main?

Thanks for your time.
rammmmm reddy
Greenhorn

Joined: Mar 24, 2005
Posts: 2
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"
sudhir harsha
Greenhorn

Joined: Jan 23, 2006
Posts: 19
Dear Aleksander,

ThankU for your explanation.

regards,
Sudhir
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
Originally posted by rammmmm reddy:
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"



But the string object is a static member of the class instead of a local variable in main. A bit hard to see with the way the code is written.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Originally posted by rammmmm reddy:
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"


This is not true. Please read my explanation below.



Originally posted by Faqeer Khudaka:
Hi Aleksander,

Threads are a bit confusing for me.

Could you please clarify how after the method invocation and after line 9 the for loop is not completely executed and why the output is not "vandeleur 0 1 2 3" only .
Second question, how can after thread t has started we move back and forth between the for loop and the main?

Thanks for your time.


Hello Faqeer,

I`ll try to explain. When you invoke start() method on a Thread object, Thread object becomes a thread of execution and moves to runnable state, what means it is ready to run, but not running and therefore it does not mean it will run exactly in the same moment you invoked start() method, it just mean it might run but we don`t exactly know when it will happen, it`s up to scheduler. That`s why the for loop may not be executed before print statement in the main method. On the other side it can iterate one, two, three or four times and sometime between program execution can move back to the main method() and print what sName reffers to in this moment. The puropse of threads is to work concurrently, or better to say wee see it like they worked concurrently, because proccessor can handle only one process at a time. The thread "t" must be able to move back to runnable state(not running) to allow main method to execute, so we can see the whole proccess as concurrent proccess, because imagine situtation when loop in run() method was to iterate 10000000000 times, if it wasn`t possible to move to main method in any time, main method would have to wait until 10000000000 iterations finish, or even more main method would have to wait until thread "t" dies. What if there was many, many more other things to execute in the "t" thread that would take a long long time? Main method would be waiting and waiting and waiting.... then there`s no concurrency. So if you have two threads, in our case one thread is a main method, second thread is "t", execution can move back and forth between them and in our case we cannot say when moving from one to another will exactly happen.

Hope this didn`t sound too confusing and sorry for my English.
Faqeer Khudaka
Ranch Hand

Joined: Jan 24, 2006
Posts: 32
Thanks Aleksander,

You have cleared "clouds of confusion" that I had on threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green Mock- doubt