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

Garbage from MindQ

 
MONZY THARIAN
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4

The Answer b .
Can any one explain me how . This takes a lot of time...
Cheers
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion the answer is '0' because all are string literals here and String literals are not garbage collected.
Do correct me if I am wrong.
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think too that the correct answer is 0.
Only to be sure, check if these are all the lines, sometimes there is one GC object, and this is the one that executes the code.
..Cristian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer is definitely 0. For the n-th time, String literals are NOT garbage collected
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
sridevi kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This contradicts with javaprepare.com tutorial "Classes in Java":
public class GCTest {
public static void main(String args[]) {
String a,b;
String c = "test";
a = c;
c = null; // The String "test" is not yet
//available for GC as a still points to "test"
b = new String("xyz");
b = c; // String "xyz" is now available for GC.
a = null;
//String "test" is now available for GC.
}
This explanation with yours confuses me. Could someone explain the correct procedure in detail?
}
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
Answer is definitely 0. For the n-th time, [b]String literals are NOT garbage collected
HIH
[/B]

Im confused too.
String literals never get collected by the GC Thread, but y question is : are they "elegible" ???
Im agree with you Valentin, but Im a little confused about the reason why String literals are never collected...
:/
----
Ive just read the topic:
Topic: Are string literals garbage collected?
And I have it clear now
Thanks
[This message has been edited by Zkr Ryz (edited November 23, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic