Isn't E wrong for case I because for the new
thread started in run, the if condition is not satisfied? id holds the name of only the thread started in the main() method and is not reassigned to the new thread's name, right?
2. public class Salmon 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 Salmon()).start();
8. throw new Error();
9. }
10. System.out.print(i + " ");
11. } }
12. public static void main(
String[] args) {
13. Thread t1 = new Salmon();
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.
Answer (for Objective 4.2):
E is correct. 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.