posted 20 years ago
The key point to this question is the call to System.gc(). When you call this method, you are asking for the JVM to run Garbage Collector. It will search for objects eligible for garbaging, and will call the finalize() method once in these objects. Note, however, that you can only suggest that the garbage collector should run, the JVM can completely ignore you, or can do just part of the work, there is no way for you to know.
So back to the question, waht happens is that after the call to m1(), we will have A1A2A3A4 printed in the console, as you would have expected, and three objets eligible for garbage colleting, pointed by a1, a2 and a3 (note that a0 is not an object, just a reference to one). So the Garbage Collector will finalize none, any or all of these objects, calling its finalize()'s methods, which prints their names. Therefore, the answers a,c,d,e are all possible to happen. Running the example in my computer I always got answer d, A1A2A3A1A1A2A3, but this doesn't mean that the others are not possible.
Hope this can be helpfull! Cheers!