aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQuest Cert Simulation Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQuest Cert Simulation Question" Watch "JQuest Cert Simulation Question" New topic
Author

JQuest Cert Simulation Question

Paul Caudle
Ranch Hand

Joined: Jun 08, 2000
Posts: 64
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).]
Jason
Ranch Hand

Joined: Dec 06, 2000
Posts: 35
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
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
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
Raj Devaraj
Ranch Hand

Joined: Jun 29, 2000
Posts: 44
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

Joined: Jun 13, 2000
Posts: 66
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

Joined: Jun 08, 2000
Posts: 64
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

Joined: Jun 08, 2000
Posts: 64
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

Joined: Jun 29, 2000
Posts: 44
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

Joined: Jun 13, 2000
Posts: 66
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!!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 08, 2000
Posts: 64
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

Joined: Jun 13, 2000
Posts: 66
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

Joined: Jun 29, 2000
Posts: 44
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

Joined: Jun 08, 2000
Posts: 64
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQuest Cert Simulation Question
 
Similar Threads
JQuest
Threads
A suggestion for list of Mock Questions
Another Question From JQuest Mock Exam...
Thread Question