This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
a) 0 b) 1 c) 2 d) 3 e) 4
The answer that they provide is b) 1.
My argument is that since all the assignments are made to the string objects in the string pool rather than heap, none of the objects will be eligible for GC till the class is unloaded. So I prefer to choose (a)
Dont worry I dont think questions like these are going to be on the exam, Mr. Bert Bates said so himself not too long ago. And you know what that means...This would also make a great interview questions
Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.