Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what stage in the following method does the string "abc" becomes available for garbage collection. Select the one correct answer.

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

a) Before statement labelled 1
b) Before statement labelled 2
c) Before statement labelled 3
d) Before statement labelled 4
IMO answer should be c, bcoz the object reference has been made equal to null at line 2 but actual answer is d, I don't knbow how. Can anybody please explain me.
Thanx in advanced.
Never.
 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible the question refers "abc" to the String literal (s). I don't know, however, if String literals are eligible for gc.
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think theres some confusion in the questionairre. you have not specified
which string "abc" becomes available for garbage collection.
keep in mind that two separate string objects are created when
you use
String r = new String("abc");
or
String s = "abc";
in this context, "abc" referenced by r will become available for garbgare
collection at line 1.
similarly, line 3 for "abc" referenced by s.
i think question demands GC availability for s object
HTH,
chetan
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic