Java OCA OCP Practice Question 2447

Question

Given:.

 2. public class Main extends Thread {   
 3.   public static void main(String[] args) {    
 4.     Thread t = new Thread(new Main());  
 5.     Thread t2 = new Thread(new Main());  
 6.     t.start();     /*from   ww w .ja  v a  2  s .co  m*/
 7.     t2.start();  
 8.   }  
 9.   public static void run() {  
10.     for(int i = 0; i < 2; i++)    
11.       System.out.print(Thread.currentThread().getName() + " ");  
12.   }  
13. } 

Which are true? (Choose all that apply.)

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


A is correct.

Note

Main does not correctly extend Thread because the run() method cannot be static.

If run() was correctly implemented, then D, E, and F would have been correct.

Thread names do NOT have to be sequentially assigned.

C is wrong even if run() is correct because only two threads are involved.

G is wrong even if run() is correct, because run() is called only once per thread.




PreviousNext

Related