Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about Garbage Collection!

 
laura_zpf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() {
r = r+1; //1
r = null; //2
s = s + r; //3String r = new String("abc");
String s = new String("abc");
} //4
a.Before statement labeled 1
b.Before statement labeled 2
c.Before statement labeled 3
d.Before statement labeled 4
e.Never.
The answer is d. But I think it should b. Because the two statements String r = new String("abc");String s = new String("abc");, the two "abc" is refer to different memory.The statement:r=r+1 lets r refer to a different memory, so I think the one of the "abc" should be collected.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura,
The question is not clear !
Where is the initial string referred by S??
The code u have given will not compile !
-sampaths
 
laura_zpf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I make a mistake. The correct question should be following.
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
a.Before statement labeled 1
b.Before statement labeled 2
c.Before statement labeled 3
d.Before statement labeled 4
e.Never.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic