aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about gc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about gc" Watch "question about gc" New topic
Author

question about gc

michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0

i think the answer is D
but the answer given is A
why?
thanks


--<br />a java beginner from China
William Ulman
Greenhorn

Joined: Feb 27, 2002
Posts: 1
"Test" needs to be gc'ed?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
This is a terrible question. First of all, you NEVER can make predictions about what is going to be garbage collected or when it will happen. The best you can do is say when an object is eligible for garbage collection.
Second, string literals are never garbage collected. The string literals "Test" and "Today" will not be garbage collected. Since there are no other objects being created in the sample code (ie, there is no "new" operator) there will be no objects eligible for garbage collection.
What test is this question from?


Rob
SCJP 1.4
michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
Originally posted by Rob Ross:
This is a terrible question. First of all, you NEVER can make predictions about what is going to be garbage collected or when it will happen. The best you can do is say when an object is eligible for garbage collection.
Second, string literals are never garbage collected. The string literals "Test" and "Today" will not be garbage collected. Since there are no other objects being created in the sample code (ie, there is no "new" operator) there will be no objects eligible for garbage collection.
What test is this question from?

this question is from Javacaps (NO 12 Question)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4745
    
    7

At least you've shown that you learned something by questioning the validity of the answer given .
Junilu
[ March 05, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
Originally posted by Junilu Lacar:
At least you've shown that you learned something by questioning the validity of the answer given .
Junilu
[ March 05, 2002: Message edited by: Junilu Lacar ]

surly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about gc