Java OCA OCP Practice Question 2481

Question

Given:

2. public class Main extends Thread {  
3.   public static long id;  
4.   public void run() {  
5.     for(int i = 0; i < 4; i++) {  
6.       // insert code here  
7.         new Thread(new Main()).start();  
8.         throw new Error();  
9.       }   /* w w  w  . j av a2s  .  c o  m*/
10.       System.out.print(i + " ");    
11.   } }  
12.   public static void main(String[] args) {   
13.     Thread t1 = new Main();  
14.     id = t1.getId();  
15.     t1.start();  
16. } } 

And the two code fragments:.

I.  if(i == 2 && id == Thread.currentThread().getId()) { 

II. if(i == 2) { 

When inserting either fragment, independently at line 6, which are true? (Choose all that apply.)

  • A. Both fragments produce the same output.
  • B. Both fragments will end in about the same amount of time.
  • C. Compilation fails, regardless of which fragment is inserted.
  • D. Regardless of which fragment is inserted, output ends once the Error is thrown.
  • E. Regardless of which fragment is inserted, output continues after the Error is thrown.


E is correct.

Note

In either case, before the Error is thrown a new thread is created, and the new thread will execute independently of the Error.

A and B are incorrect because fragment II creates a sort of recursive, endless loop.

C and D are incorrect based on the above.




PreviousNext

Related