Java OCA OCP Practice Question 2764

Question

Given:

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

Which are true? (Choose all that apply.)

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


C and D are correct.

Note

When you attempt to invoke start() on the same thread more than once, an exception is thrown and a second thread is NOT started.

That exception doesn't keep the first thread from completing.




PreviousNext

Related