Java OCA OCP Practice Question 2933

Question

Given:

3. class MyClass implements Runnable {  
4.   public void run() {  
5.     for(int i = 0; i < 2; i++)     
6.       System.out.print(Thread.currentThread().getName() + " ");  
7. } }  /*from  w  ww . j  av  a 2  s. c  o  m*/
8. public class Main {  
9.   public static void main(String[] args) {      
10.     MyClass w = new MyClass();  
11.     Thread t1 = new Thread();  
12.     Thread t2 = new Thread(w);  
13.     Thread t3 = new Thread(w, "fred");  
14.     t1.start();   t2.start();   t3.start();  
15. } } 

Which are true? (Choose all that apply.)

A.   Compilation fails.
B.   No output is produced.
C.   The output could be Thread-1 fred fred Thread-1
D.   The output could be Thread-1 Thread-1 Thread-2 Thread-2
E.   The output could be Thread-1 fred Thread-1 Thread-2 Thread-2 fred
F.   The output could be Thread-1 Thread-1 Thread-2 Thread-3 fred fred


C is correct.

Note

Only t2 and t3 have a runnable target, and t3's name will certainly be "fred", so option D can't be correct.

E and F are wrong because that output would require more than two threads with targets.




PreviousNext

Related