Originally posted by Dan Culache:
On the other hand the question states "assuming no compiler optimizations are done" so I guess that means that a,b,c,d should be treated not like Strings but like any other objects which makes c the correct answer.
But it's good to know that we won't find any questions about garbage collecting String objects on the real exam :-)
Dan,
Compiler optimizations can cause objects to be garbage collected much earlier than one might expect. For example, a compiler optimization might cause an object to be garbage collected at a point in the program where it is still referenced but is clearly not going to be used any further. If you place a print statement in an object's finalize method then you can see indications of unexpected--but entirely logical-- garbage collection.
It is very important to remember that the behavior of the garbage collection mechanism is not defined by the
Java Language Specification and no gc optimization will appear on the exam. For that reason,
you should not attempt to answer questions based on undocumented gc optimizations. Instead, just remember the familiar rules about when objects become eligible for garbage collections--objects that are out of scope, no remaining references to the object in live threads, etc.