Click the Exhibit button. 10. public class Transfers { 11. public static void main(String[] args) throws Exception { 12. Record r1 = new Record(); 13. Record r2 = new Record(); 14. doTransfer(r1, r2, 5); 15. doTransfer(r2, r1, 2); 16. doTransfer(r1, r2, 1); 17. // print the result 18. System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get()); 19. } 20. private static void doTransfer( 21. final Record a, final Record b, final int amount) { 22. Thread t = new Thread() { 23. public void run() { 24. new Clerk().transfer(a, b, amount); 25. } 26. }; 27. t.start(); 28. } 29. } 30. class Clerk { 31. public synchronized void transfer(Record a, Record b, int amount){ 32. synchronized (a) { 33. synchronized (b) { 34. a.add(-amount); 35. b.add(amount); 36. } 37. } 38. } 39. } 40. class Record { 41.int num=10; 42. public int get() { return num; } 43. public void add(int n) { num = num + n; } 44. } If Transfers.main() is run, which three are true? (Choose three.)

  1. The output may be “r1 = 6, r2 = 14”.

  2. The output may be “r1 = 5, r2 = 15”.

  3. The output may be “r1 = 8, r2 = 12”.

  4. The code may run (and complete) with no output.

  5. The code may deadlock (without completing) with no output.

  6. M IllegalStateException or InterruptedException may be thrown at


Correct Option: A,B,E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The output may be "r1 = 6, r2 = 14" - This option is true. In the given code, the doTransfer method is called three times, each time transferring a certain amount from one Record object to another. As the transfer method in the Clerk class is synchronized, only one thread can execute the transfer at a time. In the first transfer (doTransfer(r1, r2, 5)), the value of r1 is decreased by 5 and the value of r2 is increased by 5. Therefore, after the first transfer, the value of r1 will be 5 and the value of r2 will be 15. In the subsequent transfers, the values will be updated accordingly, resulting in "r1 = 6, r2 = 14" as a possible output.

Option B) The output may be "r1 = 5, r2 = 15" - This option is true. As explained in option A, after the first transfer, the value of r1 will be 5 and the value of r2 will be 15. Therefore, "r1 = 5, r2 = 15" is a possible output.

Option C) The output may be "r1 = 8, r2 = 12" - This option is false. The given code does not involve any transfers or operations that would result in r1 becoming 8 and r2 becoming 12.

Option D) The code may run (and complete) with no output - This option is true. The code may execute without any errors and complete without printing any output.

Option E) The code may deadlock (without completing) with no output - This option is true. Deadlock can occur in a multi-threaded program when two or more threads are waiting for each other to release a resource. In this code, there are multiple synchronized blocks and multiple threads running concurrently. If a deadlock occurs, the code may get stuck and not complete, resulting in no output.

Option F) IllegalStateException or InterruptedException may be thrown - This option is false. There are no specific conditions in the given code that would cause an IllegalStateException or InterruptedException to be thrown.

Therefore, the correct answers are A, B, and E.

Find more quizzes: