*
The moose likes Mock Exam Errata and the fly likes Another doubt in Sarga's Test2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Another doubt in Sarga Watch "Another doubt in Sarga New topic
Author

Another doubt in Sarga's Test2

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Hi,
Check following question and please guide me accordingly.
Thanx Once Again
25)
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12

Answer : d,means at line 10.
My Answer: When Object is getting null value at line 11,then how can it be avilable for garbage collection before i.e. at line 10.??


Swati Kale
SCJP
SCWCD
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Swati,
Line 9, <code>anObj = anObj.toUppercase()</code> will return a new String object; not the original (remember Strings are immutable).
Assuming literal strings are available for gc, the original string "sample" will no longer be referenced and hence eligible for gc.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Asma Zafar
Ranch Hand

Joined: May 11, 2001
Posts: 49
But the question should have been, at what line the object created at line 5 becomes available for garbage collection, instead of when anObj is available for garbage collection, for that the answer should be line 12.


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Asma,
You're right. The question is worded poorly. 'anObj' is a reference variable not an object, so it would never be eligible for gc. The String object "Sample" would be available for gc at line 10.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
yaeger rosenberg
Greenhorn

Joined: Aug 04, 2001
Posts: 6
if the String had been "A123" then toUpperCase() will not return a new reference (since the original reference is returned for cases in which the uppercase string is identical to the original), and the original String would not be eligible for gc?
And what is the rule about gc of String literals? Is there any difference in the language specs in regard to String literal gc and other object gc?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Yaeger,
Your comment on <code>"A123".toUpperCase()</code> is correct. If any of the String methods can be satisfied by the existing String, then a reference to the original String object is returned.
The JVM specifications do not require the gc of string literals; however, many of the existing mocks use them in their gc questions, so when answering, assume they are gc'd.
The real exam only tests you on system independant aspects of the language i.e. you won't see any questions in which it is necessary to know what underlying os is being used.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another doubt in Sarga's Test2
 
Similar Threads
Can anyone help
Garbage collection..
Garbage Collection
GC
Garbage collect from Sarga.com Exam