|
1. þ B
The very first object is created on line 10. In the method garbageTest(), total 3 objects are created, two Date objects and one String object. All these three objects are eligible for garbage collection when the method returns. The object referenced by tester is not yet eligible on line 11.
2. þ E
None of the statements is true. A is not true as you cannot be sure about how the garbage collection is implemented. B is not true as garbage collection makes no such guarantees. C is incorrect as we have seen in cyclic references that an object can become garbage even when another object has a reference to it. D is incorrect as presence of finalize() does not increase or decrease the chances of garbage collection.
3. þ C
C is true. A is not true as you cannot be sure about how the garbage collection is implemented. B is not true as garbage collection will make an effort to make available the memory but it does not guarantee it. Moreover, you can not be sure that garbage collector will run after making a call to System.gc(). D is incorrect because object doe not become unreachable just because it is referring to an unreachable object.
4. þ D
Total 3 objects are created. The first two are referenced by t1 and t2 respectively on line 4 and 5. The second object (which is created on line 5) becomes eligible for garbage collection on line 8 and the first object (which is created on line 4 ) becomes eligible for garbage collection on line 9. This is an example of cyclic references involving 2 objects.
(Tip: It is very easy to answer these types of questions if you draw a diagram (memory model) on scrap paper as you go through the code.)
5. þ D E
If you draw the memory model, you can see that only t4 is referring to the object created on line 5. Therefore if you reassign t4 to some other object (option E) or if you set it to null (option D) then the object created on line 5 has no references and hence it becomes eligible for garbage collection.
6. þ D
A is incorrect as the reference of the object created on line 4 is returned from the method testDrive(). It is assigned to variable mycar. At line 11, this variable is reassigned and is no longer referring to the object created on line 4. Since this object now has no reference pointing to it, it becomes eligible for garbage collection.
7. þ D
The finalize() method of an Car object is called by the garbage collector just before it collects that object. The object can be garbage-collected anytime after it becomes eligible for it or it may not be collected at all. The first Car object becomes eligible at line 5. But that does not mean the garbage collector will run immediately. Note that we cannot predict exactly when the finalize() will be called or whether it will be called at all.
8. þ C
In total, 3 objects are created in the method. Two String object are created on lines 4 and 6 using the new keyword. The string literal “1994” is used first time at line 5, creating another string object. When the method returns all these objects are eligible for garbage collection. Note that all method variables are destroyed after the method exited and hence there will not be any root references to the objects created in the method.
9. þ E
The car objects created on line 8 are immediately eligible for garbage collection, as they do not have any reference to them. However, exactly when they will be garbage colleted is not known. Therefore we cannot say for sure exactly how many times the finalize() will be called (by the garbage collector) or even it will be called at all.
D is incorrect as System.gc() does not guarantee that garbage collection will occur. Therefore we cannot say for sure how many times the finalize()