Multiple choice technology programming languages

Given: 1. class A { 2. } 3. class Alpha { 4. private A myA = new A(); 5. 6. void dolt( A a ) { 7. a = null; 8. } 9. void tryIt() { 10. dolt( myA ); 11. } 12. } Which two statements are correct?

  1. Explicitly setting myA to null marks that instance to be eligible for garbage collection

  2. Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.

  3. Any call on tryIt() causes the private instance of A to be marked for garbage collection.

  4. There are no instanced of A that will become eligible for garbage collection.

Reveal answer Fill a bubble to check yourself
A,B Correct answer
Explanation

The private A instance (myA) can only become eligible for GC when the Alpha instance containing it becomes eligible. Option A is misleading - explicitly setting myA to null within tryIt() doesn't happen (the method sets a local parameter to null, not myA itself). Option B is correct - when Alpha is GC'd, its private myA field becomes unreachable and eligible for GC. Option C is false - tryIt() doesn't affect myA's reachability.