File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Garbage collection " Watch "Garbage collection " New topic
Author

Garbage collection

H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.


void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4



Before statement labeled 1
Before statement labeled 2
Before statement labeled 3
Before statement labeled 4
Never.

Can someone explain what will be the answer to this question and why ?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
My Answer is "Never".
To be more correct it's eligible for garbage collection after "methodX()" get's executed.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Sisir Chanda
Greenhorn

Joined: Jan 03, 2005
Posts: 11
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
I disagree. The correct answer is "Before statement labeled 4". The reason is that "the object initially referenced by s" is a String object with the value of "abc". In statement 3, a String object is created with the value "abcnull" and that String is then assigned to s. As a result, the "abc" String that used to be referenced by s is no longer referenced by anything, so the object initially referenced by s is eligible for garbage collection as soon as statement 3 completes, which is immediately before statement 4.


SCJA 1.0 (98%), SCJP 1.4 (98%)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Sisir Chanda:
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.


where u have read this about...
give me the linke....

i think joe sanawitz is perfectly right and i have seen that explanation every where but your statement is to be honest strange..

anyway give me the link from where you have read this..


Thanks and Regards, Amit Taneja
Sisir Chanda
Greenhorn

Joined: Jan 03, 2005
Posts: 11
yep...It was my mistake...I read that the equestion is '...when the object refernced by s is eligible for GC...'. Since before line 4 , '....s = s + r; //3
...' was used, so my answer was s will never be eligible for GC. But the actual question was '....when the INITIAL object referenced by s will be eligible for GC.....'

The the answer is definitely....Before statement labeled 4. since at line 3 s has been assigned to a different object and s is referring to a different object. Having said that, the INITIAL object reference by s will become eligible for GC.

Thanks guys!!!
 
GeeCON Prague 2014
 
subject: Garbage collection