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).]
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 ?
Joined: Jun 30, 2000
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).]
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?