Which two are possible results? (Choose two.)

Refer to the Exhibit.

1. public class Threads1 {
2. int x = 0;
3. public class Runner implements Runnable {
4. public void run(){
5. int current = 0;
6. for(int i = 0; i<4; i++){
7. current = x;
8. System.out.println(current + “, “);
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args) {
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable r1 = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23. }

Which two are possible results? (Choose two.)

Refer to the Exhibit.

1. public class Threads1 {
2. int x = 0;
3. public class Runner implements Runnable {
4. public void run(){
5. int current = 0;
6. for(int i = 0; i<4; i++){
7. current = x;
8. System.out.println(current + “, “);
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args) {
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable r1 = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23. }

Which two are possible results? (Choose two.)

A.
0, 2, 4, 4, 6, 8, 10, 6,

B.
0, 2, 4, 6, 8, 10, 2, 4,

C.
0, 2, 4, 6, 8, 10, 12, 14,

D.
0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

E.
0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

Explanation:
A: 0, 2, 4, 4, 6, 8, 10, 6,
1 1 1 2 2 2 2 1 <- possible threads that produced this output – possible solution

B: 0, 2, 4, 6, 8, 10, 2, 4,
1 2 2 2 2 ? 1 <- to print second ‘2’, T1 interrupted between L10/L11; 4 passes of T2 used up

C: 0, 2, 4, 6, 8, 10, 12, 14,
1 1 1 1 2 2 2 2 <- possible solution – simplest solution (T2 waits until T1 is completely done) – doesn’t matter that it isn’t likely, just that is possible

D: 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
1 2 1 2 1 2 1 2 1 2 ? <- threads used up

E: 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
1 1 1 1 2 2 2 2 ? <- threads used up



Leave a Reply 1

Your email address will not be published. Required fields are marked *