• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQuest Cert Simulation Question

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the question:
What can you write at the comment //A in the following code so that this program writes the word "running" to the standard output?
1. class RunTest implements Runnable {
2. public static void main(String args[]) {
3. RunTest rt = new RunTest();
4. Thread t = new Thread(rt);
5. //A
6. }
7. public void run() {
8. System.out.println("running");
9. }
10. void go() {
11. start(1);
12. }
13. void start(int i) {
14. }
15. }
Select all valid answers.
a. System.out.println("running");
b. rt.start();
c. rt.go();
d. rt.start(1);//corrected typo here
I see a as the only correct one. The answer key gives both a and b as correct answers. What is it that I am missing. Anything simple? I feel like a dummy, but that is not an unusual feeling for me
Anyway, if you can justify b please do, if you can make me feel less like a dummy that would be greatly appreciated, too.
Thanks for the help.
-Paul

[This message has been edited by Paul Caudle (edited June 29, 2000).]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Paul;
both a and b are correct, as well d if you do not make a mistake there (b and d are identical).
A is right because it is just normal execution of main thread.
B is also correct because main thread spawns a new thread (remember it implements runnable), so start() method will invoke the run () since you already define it in the class.
Hope this will help.
cheers
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try going through this : http://java.sun.com/products/jdk/1.2/docs/api/index.html
There are two ways to create and start a thread.
1) by extending => class A extends Thread
2) by implemeting interface Runnable => class A implements Runnable
When u say rt.start(), the run method gets invoked because "rt" implements Runnable.
Another valid answer could be "t.start()" because t is a thread which has a Runnable parameter i.e. "rt"
I hope I haven't confused you more than u already were!! :-)
But, I would recommend u going thru the API for Thread. It is explained nicely there.Let me know if u find something wrong.
A and B should be the right answers. What about D? Is it same as D or it has a parameter within ?
Ankur
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur,
Which start is rt.start() starting. I don't recall that Object has a start method. I am sure Runnable has only void run() method. The start method in the class RunTest class takes an int parameter. So if RunTest is not extending Thread and t.start() is not an option, I don't think other than option a is a valid answer. Please clear my doubt.
Thanks
Raj
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The start method is in the Thread class.
Following is from the API
public class Thread
extends Object
implements Runnable
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to
have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with
lower priority. Each thread may or may not also be marked as a daemon. When code running in some
thread creates a new Thread object, the new thread has its priority initially set equal to the priority of
the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically
calls the method named main of some designated class). The Java Virtual Machine continues to
execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted
the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run
method or by throwing an exception that propagates beyond the run method.
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of
Thread. This subclass should override the run method of class Thread. An instance of the subclass
can then be allocated and started. For example, a thread that computes primes larger than a stated
value could be written as follows:
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}

public void run() {
// compute primes larger than minPrime
. . .
}
}

The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();

The other way to create a thread is to declare a class that implements the Runnable interface. That
class then implements the run method. An instance of the class can then be allocated, passed as an
argument when creating Thread, and started. The same example in this other style looks like the
following:
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}

public void run() {
// compute primes larger than minPrime
. . .
}
}

The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();

Every thread has a name for identification purposes. More than one thread may have the same name.
If a name is not specified when a thread is created, a new name is generated for it.
Hope this was useful
Ankur
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the typo with selection d, folks. I have changed that to rt.start(1) as it should have been.
I am still a little confused and I think Raj has expressed the same confusion. If b is correct, how did rt get a start method? There was start(int) but no start(). I understand that there is a start() in Thread, so that t.start() would have worked, but I still cannot see why rt.start() will work?
Any help in this would be appreciated
Thank, Folks.
-Paul
[This message has been edited by Paul Caudle (edited June 29, 2000).]
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried to compile it using rt.start() as I should have done a while ago and I get a compile error:
No method matching start() found in class RunTest.
rt.start();
^
t.start() obviously works here, but not rt.start(). It is possible that I had done something wrong but either way I'm still confused
Thanks for your help and replies so far, everyone. It is greatly appreciated
-Paul
 
Raj Devaraj
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
I was about to respond to Ankur's reply. He has given all the details about the threads. But my question was specific to t.start() and rt.start(). I feel, option a is the only answer for this question and the choice of answers. Any Gurus can help me and Paul?.
Ankur: Thanks for good explanation.
Thanks
Raj
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using this pgm:
public class athread extends Thread implements Runnable
//public class athread implements Runnable
{
public static void main(String[] args)
{
athread a = new athread();
a.start();
System.out.println("end a.start() method");
athread b = new athread();
b.start(3);
System.out.println("end b.start(3) method");
athread c =new athread();
Thread t = new Thread(c);
System.out.println("Before t thread");
t.start();
}
public void start()
{
System.out.println("in overridden start ");
this.run();
}
public void start(int i)
{
for(int j=i; j>0;j--)
System.out.println("in loop start(" + j + ") ");
this.run();
}
public void run()
{
System.out.println("in run()" );
}
}
Just for the sake of example I have extended Thread and Implemted Runnable in the same class. This is not actually how u would normally do it. But, just for the sake of understanding!!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, everyone!
I think the answer should be
a. System.out.println("running");
b. t.star();(if b is t.start()
rt.star(); is not a correct answer because rt has no method start() though it implements Runnable. If the program wants to invoke a thread, it must pass the rt as a parameter to creat an t, an instance of Thread. so the t has the method start(), then the thread can be run.
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation, Ankur, it is greatly appreciated.
I think the confusion, at least for me, lies in the fact that the RunTest class has no start() method and does not inherit from Thread. Selection b in the original post attempts to call the start method of RunTest but I can't see where start() in RunTest comes from since it does not extend Thread.
Thanks for your help,
Paul
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rt.start() is wrong because "rt" implements Runnable. It does not extend Thread !! shucks!! I just overlooked it. Mistakes just seem to keep on happening!! If it does not extend Thread how will it inherit the method start()!! Anyways, Slippery one that was!!
 
Raj Devaraj
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Ankur and Mikeliujy,
Ankur your explanations were very good. What I was looking for was what Mikeliujy mentioned. Option b is correct if it is t.start() and not rt.start(). Thanks to everyone for feedback
Raj
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to everyone for clearing that up. My faith in that cert sim is not quite 100% anymore. Oh well, there are lots of other certs in the sea.
I appreciated all the help and discussion as well as thread information because of this. It was well worth it to post to this site. Have a good one, y'all
-Paul
 
reply
    Bookmark Topic Watch Topic
  • New Topic