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 Another question from Khalid 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 "Another question from Khalid" Watch "Another question from Khalid" New topic
Author

Another question from Khalid

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Yesterday I was throwing some questions for Khalid's mock exam out here because I found that mock to be the most difficult mock exam out there. I have been going through the mock a question at a time making sure I understand every point fully. In going with that, I thought I would throw some questions out here.
You can do a search for Khalid and posts by me (bill bozeman) to see some of the other questions and discussions.
Here is another one:

9. Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?

Select all valid answers:
A. Code block A
B. Code block B
C. Code block C
D. Code block D
E. Code block E

Again, try to figure out the answer, then try to compile it and run it to see what you get.
Hope these help some, I know they helped me.
Bill
[This message has been edited by bill bozeman (edited December 21, 2000).]
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Bill,
When i answered the test i gave the option as A. But when i complied and saw the code it is giving me an error. Are all the answers wrong or what? Please clarify.

Thank u
rajani

[This message has been edited by rajani peddi (edited December 21, 2000).]
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
Bill I only like A, thats the only one that makes the correct use of the start() and run() methods, do you need more explanation on this ?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
rajani, post your code that tried to use so we can see it. A should be the only correct answer.
sarim, you don't need to post more detail as long as you understand why A is correct and why the rest are not.
Here is my reasoning:
A true - This creates an anonymous class that implements Runnable, creates a thread passing in the implentation of Runnable, then starts the thread.
B false - This creates an anonymous class that extends Thread, but they don't override Thread's run method, so the Thread never starts. Since you are overriding Threads start method, it will run, but it is not creating a Thread and calling Thread's start method which schedules it with the Thread Schedular.
C false - This creates an anonymous class that implements Runnable, but never creates the Thread so you will get a compile error as r has no start() method.
D false - This creates a new Thread where you pass in an object Work, but there is no constructor in the Thread class that supports objects unless the object implements Runnable, which Work never said it did. You can pass in an object that implements Runnable, a string for the object name, or a ThreadGroup object
E false - The creates an anonymous class that implements Runnable, but it doesn't create a new Thread and call the start method of the new thread. This will compile, but it doesn't create a new thread.
Just my thoughts on the answers. This one I got right the first time through (one of only a few that I got right the first time through )
Bill

[This message has been edited by bill bozeman (edited December 21, 2000).]
ali farid
Ranch Hand

Joined: Aug 26, 2000
Posts: 60
The code A,B and E are compiling successfully and call the doIt() but the code A is starting a new thread.
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Bill,
I made a mistake in writing the program. When you have asked me to post it here i have double checked it and then found my error.
Thank u
rajani
Tejovikas Suravajjala
Greenhorn

Joined: Dec 21, 2000
Posts: 4
Good question Bill ..
Folks,
Here is what I think..correct me if I am wrong
A: Correct Answer
B: Incorrect and the trap. Starts a new thread but the work is not done by that thread.
C:Compile Error.
D:Compile Error.
E:Where is my thread?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another question from Khalid