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.
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.)