Hi nitin,
Technically, no object would be available for garbage collection (ans 6). Two objects are created in the String pool, "Java" and "JavaScript". These would not be garbage collected unless the system they running on had optimized the JVM to run the garbage collector on the String pool.
A third object 't' is created in <code>main()</code> however it's reference is never set to 'null'. The program ends, at which point nothing is gc'd.
If we assume the mock exam author treated string literals as objects eligible for gc (which is usually the case), I'm not sure how they arrived at the answer of 'one object being eligible for gc'.
There are two strings created in the String pool. "Java" and "JavaScript". The reference variable 's' originally points to "Java" then is reset to point to a new string "JavaScript". The statement <code>String x = "Java";</code> will not create a second "Java" object; a reference to the existing pool object will be returned. Reference 'y' is then given x's reference, so it will now point to the original string object "Java".
At the end of the method you end up with 'y' holding a reference to "Java" and 's' holding a reference to "JavaScript".
If string constants were eligible for gc; both these objects would be available when the method completes.
Most of the mock exams ignore the fact that gc is system dependent and use literal strings for the gc examples. For the purpose of the mocks, the majority assume String literals are collected. However;
it is highly unlikely you will get a gc question on the exam that involves String literals!. Sun will not include questions that rely on knowledge of a specific JVM implementation!
Also, don't get yourself in knots over gc ... the majority of people report they only had
one gc question on the exam
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform