Java OCA OCP Practice Question 2556

Question

Given:

2. public class Main extends Thread {  
3.   static Thread t1, t2, t3;  
4.   public static void main(String[] args) throws Exception {  
5.     t1 = new Thread(new Main());  
6.     t2 = new Thread(new Main());  
7.     t3 = new Thread(new Main());  
8.     t1.start();   t2.start();   t3.start();  
9.   }  /*from   www  .j  a v a2  s .c  om*/
10.   public void run() {   
11.     for(int i = 0; i < 500; i++) {  
12.       System.out.print(Thread.currentThread().getId() + " ");  
13.       if(i == 250)  
14.         try {   
15.           System.out.print("**" + t1.getId() + "**");  
16.           t1.sleep(600);   
17.         }  
18.         catch (Exception e) { }  
19. } } } 

Which are true? (Choose all that apply.)

A.Compilation fails.
B.An exception is thrown at runtime.
C.Main will execute for a second or two.
D.Main will execute for at least 10 minutes.
E.Thread t1 will almost certainly be the last thread to finish.
F.Thread t1 will almost certainly be the first thread to finish.
G.It's difficult to predict which thread will be the last to finish.


C and G are correct.

Note

The sleep() method's argument is in milliseconds.

E is incorrect because sleep will be called once on each of the three threads, not on Thread t1 three times.

Remember, the sleep() method is static and operates on whichever thread is the currently running thread.




PreviousNext

Related