wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A GC Problem in Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A GC Problem in Mock Exam" Watch "A GC Problem in Mock Exam" New topic
Author

A GC Problem in Mock Exam

Andrew Cheng
Greenhorn

Joined: Jan 09, 2002
Posts: 12

What is the earliest point where the memory space allocated to the
string in line 2(s1) can be released.
(A) before line 6
(B) before line 7
(C) before line 8
(D) before line 5
ans:A it will destroy the existing string and create new string
because strings are immutable
The text above is the question,answer and explain.
But I think B is correct answer.
Who is wrong?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
It doesn't appear that variable s1 gets a new value until *after* line 6:
s1 = null;
The line:
s2+=s1 is the same as s2 = s2 + s1;
This creates a new string intance but assigns it to s2, and s1 still keeps pointing to what it had before.
unless there is a call on line 4 that didn't get copied over, I can't see how answer A is correct. I would say B is correct.

Rob


Rob
SCJP 1.4
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
The answer is correct ie. before line 6.
initially s1->"Hi"
S2->"There";
because of the statement, s2 +=s1...
which gets resolved to....
s2 = s2 + s1
ie. s2 = "There" + "Hi"
Strings are immutable, so the initial object refered by s2 ie."There" becomes unreferenced. The + operator returns a new object which gets assigned to s2 at line 6.
as a result , s2->"ThereHi"
s1->"Hi"
So initial object refered by s2 has no reference at line 6. Hence the answer.
HIH
Rashmi
[ January 16, 2002: Message edited by: Rashmi Gunjotikar ]
Mike Beaty
Greenhorn

Joined: Dec 19, 2001
Posts: 20
The string in question would be s1, not s2.
If we were talking about s2, A would be correct.


Mike Beaty<br />Sun Certified Programmer for the Java 2 Platform
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
ohhhh ya...the question shuld be about string s2 and not s1, then a is correct.
Sorry!
my mistake, i did't see it properly.
Rashmi
[ January 16, 2002: Message edited by: Rashmi Gunjotikar ]
Silver
Greenhorn

Joined: Jan 16, 2002
Posts: 4
I think B is right for this question. But if the question is about String s2 declared in line 3, A is correct.
Suraj Berwal
Greenhorn

Joined: Dec 28, 2001
Posts: 26
dear friends,
i also think b is the right choice. if the source could be known then the question may be rechecked. well i think u should drop a line to the author of the test about this mistake.
keep exploring, have fun
suraj
Andrew Cheng
Greenhorn

Joined: Jan 09, 2002
Posts: 12
Thanks all!I Get it!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi all
Search the Ranch (by my name) to see a program that shows that String literals are not g.c.
This is so because they are placed in a pool from which there is no known way to release them.
Search the Ranch for string literals.
The conclusions are right if we consider the objects created with new String("...") constructor.
Don't worry, you won't be asked like this in the exam. They only test the reachability of String objects created with the constructor. The don't test String literals reachability.
It just happens that we are that clever in the Ranch


SCJP2. Please Indent your code using UBB Code
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
then what is the correct answer here. because the Strings in question are not created by new operator ?
Originally posted by Jose Botella:
Hi all
Search the Ranch (by my name) to see a program that shows that String literals are not g.c.
This is so because they are placed in a pool from which there is no known way to release them.
Search the Ranch for string literals.
The conclusions are right if we consider the objects created with new String("...") constructor.
Don't worry, you won't be asked like this in the exam. They only test the reachability of String objects created with the constructor. The don't test String literals reachability.
It just happens that we are that clever in the Ranch
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No answer is correct.
The string objects created by string literals will not be g.c.
[ January 17, 2002: Message edited by: Jose Botella ]
Andrew Cheng
Greenhorn

Joined: Jan 09, 2002
Posts: 12

Although the question in the mock exam is not very exactly,we learn a lot from it.It is enough,Thanks again.
kanuck java guy
Greenhorn

Joined: Jan 17, 2002
Posts: 4
I believe NO answer is correct, why? Because all the string literal are created not on the heap, but in the "Runtime constant pool!!!"
Also note, the new operator was not used to create the String literal on the heap! So both string literal "Hi" and "There" will exist for the lifetime of the program!
Don't believe me try this (you will see that s1 != s2, but s1 == s3)
public class foo {
public static void main(String[] arg) {
String s1 = "Hello";
String s2 = new String("Hello");
String s3 = "Hello";
System.out.println("s1==s2 : " + (s1==s2));
System.out.println("s1==s3 : " + (s1==s3));
}
}
The key to understanding the program is to notice that not new string in created for s3, rather it is given the SAME reference that string s1 has for the string living in the Runtime constant pool!!!
But for all stupified EXAM purposes you can assume s1, s2 and s3 point to strings created on the heap, since there are so many bad examples out there I guess SUN was willing to overlook this one
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Andrew Cheng:

What is the earliest point where the memory space allocated to the
string in line 2(s1) can be released.
(A) before line 6
(B) before line 7
(C) before line 8
(D) before line 5
ans:A it will destroy the existing string and create new string
because strings are immutable
The text above is the question,answer and explain.
But I think B is correct answer.
Who is wrong?


At line 6, there are no longer any references to the string literal "Hi." All strings that previously referenced this literal no longer do. At this point, the string literal object "Hi" is eligible for garbage collection. It doesn't mean it will definately be garbage collected, just eligible. So the answer to the original question is B.
The answer given makes no sense for this question...it's almost like it's an answer for a different question... such as "what is the result of the string assignement on line 5?"
Rob
 
wood burning stoves
 
subject: A GC Problem in Mock Exam