This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Remember that local variables are stored on the stack and GC doesn't apply to them. Local variables dissolve automatically when the method ends.
In this case, a new string object s1 is created in method f(). s1 gets assigned s, so another string object is created, but in the array. The same applies to s1. When f() returns s1, there are 3 object: the s1 array and the two array elements assigned with String objects.
For anyone keeping score, remember that on the real exam GC questions will never use objects of type String. To keep things simple, whenever you see a mock GC question that uses objects of type String, substitute objects of another type. And if you see the answer for a mock exam discuss the String constant pool - relax - that's way outside the scope of the real exam.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)