| 9. | Which three guarantee that a thread will leave the running state? 1 yield() |
|||||||
Answer: Option B Explanation: (2) is correct because wait() always causes the current thread to go into the object's wait pool. (5) is correct because sleep() will always pause the currently running thread for at least the duration specified in the sleep argument (unless an interrupted exception is thrown). (6) is correct because, assuming that the thread you're calling join() on is alive, the thread calling join() will immediately block until the thread you're calling join() on is no longer alive. (1) is wrong, but tempting. The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state. (3) and (4) are incorrect because they don't cause the thread invoking them to leave the running state. (7) is wrong because there's no such method. |
| 10. | Which two of the following methods are defined in class Thread? 1 start() |
|||||||
Answer: Option A Explanation: (1) and (4). Only start() and run() are defined by the Thread class. (2) and (3) are incorrect because they are methods of the Object class. (5) is incorrect because there's no such method in any thread-related class. |
| 11. | Which cannot directly cause a thread to stop executing? |
|||||||
Answer: Option C Explanation: Option C is correct. notify() - wakes up a single thread that is waiting on this object's monitor. |
| 12. | class X implements Runnable Which of the following line of code is suitable to start a thread ? |
|||||||
Answer: Option C Explanation: Option C is suitable to start a thread. |
| 13. | Which three are methods of the Object class? 1 notify(); |
|||||||
Answer: Option C Explanation: (1), (2), and (6) are correct. They are all related to the list of threads waiting on the specified object. (3), (5), (7), and (8) are incorrect answers. The methods isInterrupted() and interrupt() are instance methods of Thread. The methods sleep() and yield() are static methods of Thread. D is incorrect because synchronized is a keyword and the synchronized() construct is part of the Java language. |
| 14. | Which two are valid constructors for Thread? 1 Thread(Runnable r, String name) |
|||||||
Answer: Option C Explanation: (1) and (2) are both valid constructors for Thread. (3), (4), and (5) are not legal Thread constructors, although (4) is close. If you reverse the arguments in (4), you'd have a valid constructor. |
| 15. | What is the name of the method used to start a thread execution? |
|||||||
Answer: Option B Explanation: Option B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. Option A is wrong. There is no init() method in the Thread class. Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread. Option D is wrong. The resume() method is deprecated. It resumes a suspended thread. |
