Home > Java Programming > Threads > Questions and Answers
01. |
What will happen when you attempt to compile and run the following code? 1. public class Test extends Thread{ 2. public static void main(String argv[]){ 3. Test t = new Test(); 4. t.run(); 5. t.start(); 6. } 7. public void run(){ 8. System.out.println("run-test"); 9. } 10. } | |||||||||||
|
02. |
Which of the following are methods of the Thread class? 1) yield() 2) sleep(long msec) 3) go() 4) stop() | |||||||||||
|
03. |
What notifyAll() method do? | |||||||||||
|
04. |
Predict the output? 5. try{ 6. throw new Exception(); 7. }catch(Error e){ 8. System.out.print("EXCE"); 9. } | |||||||||||
|
05. |
1. class A implements Runnable { 2. int I; 3. public void run () { 4. try { 5. Thread.sleep(5000); 6. i=10; 7. }catch(InterruptedException e) {} 8. } 9. } 10. 11. public class Test { 12. public static void main (String args[]) { 13. try { 14. A a = new A(); 15. Thread t = new Thread(a); 16. t.start(); 17. 18. int j= a.i; 19. 20. }catch (Exception e) {} 21. } 22. } Which statement at line 17 will ensure that j=10 at line 19 | |||||||||||
|
06. |
Which of the following method can be a override method of public void run() | |||||||||||
|
07. |
Which two CANNOT directly cause a thread to stop executing? (Choose Two) A. Calling the yield method.
B. Calling the wait method on an object. C. Calling the notify method on an object. D. Calling the notifyAll method on an object. E. Calling the start method on another Thread object. | |||||||||||
|
08. |
Which statement is true? A. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution. B. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution. C. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call. D. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
| |||||||||||
|
09. | Which of the following statements are true? | |||||||||||
|
10. |
Given the following, 1. class MyThread extends Thread { 2. 3. public static void main(String [] args) { 4. MyThread t = new MyThread(); 5. Thread x = new Thread(t); 6. x.start(); 7. } 8. 9. public void run() { 10. for(int i=0;i<3;++i) { 11. System.out.print(i + ".."); 12. } 13. } 14. } What is the result of this code? | |||||||||||
|
11. |
Which of the following statements are true. A. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state. B. The wait(), notify(), and notifyAll() methods must be executed in synchronized code. c. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state. d. The Thread class is an abstract class. | |||||||||||
|