aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes MindQ #36 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "MindQ #36" Watch "MindQ #36" New topic
Author

MindQ #36

Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61

I think the answer is (a).....no objects are elligible for
garbage collection. But the answer given was (b). Can
anyone help? Thanks!
(Original question)
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
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
They are tring to say that "Nick" is eligble for GC and that is why 1 is correct. I don't think they are write either because strings created without the new keyword are created in the string pool and are not eligble for gc. So I would say 0 also.
Bill
Tina Ang
Ranch Hand

Joined: Mar 12, 2002
Posts: 58
Hello! I am not convinced that the answer is b) 1. I also agree with them the correct answer is a) 0. Could anyone please correct us if we're wrong.
Thank you,
TINA
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
The way I see it, the first (newName) will be available for gc as soon as it is assigned the new value of "Jason" (hence the one available for GC) name is not because it is referenced by newestName (and therefore not available for gc) newestName is not ofcourse because it is referencing name.
I think the answer (b) is correct.
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
HI
John,the answer is 0 as String literals are never
garbage collected , they always have a reference to them in the String constant table while the
class is loaded and hence they dont get garbage collected.
The Exam actual exam won't test u on GC of string literals( so dont worry ).
Swapna
SCJP


SCPJ2
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
very well. someone should inform the author of that mock exam
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hi.
I think the answer is (b)
From JLS - Section 12.5
A new class instance is explicitly created when evaluation of a class instance creation expression (�15.9) causes a class to be instantiated.
A new class instance may be implicitly created in the following situations:
Loading of a class or interface that contains a String literal (�3.10.5) may create a new String object to represent that literal. (This might not occur if the same String has previously been interned (�3.10.5).)
Here's the excerpt from 3.10.5 -
Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern
-----------------------------------
Thats JLS, this is what I understand from all that -
Whenever a string literal is encountered ( such as - String s = "Hi"; ), a new object is created in the literal pool IF there is no such literal already present.
So, coming back to the question asked -
String name; // line 1
String newName = "Nick"; //line 2
newName = "Jason"; // line 3
name = "Frieda"; // line 4
String newestName = name; // line 5
name = null; // line 6
//Line A
line 1 - No object is created here. Just a reference 'name' is created.
line 2 - One object, value = "Nick", is created.
line 3 - The reference pointint to "Nick" now points to a newly created Object of type String with value "Jason". So, at this point, No reference is pointing to "Nick".
line 4 - One more new String object is created with value "Frieda" & name points to it now
line 5 - Another new reference is created and it points to the same object "Frieda", to whom reference 'name' is pointing to. So, in this case, no new String object is created
line 6 - The reference 'name' is assigned null. But, it has no effect on the string Frieda, since it is still being pointed to by reference 'newestName'.
So, after line 6, only 1 object ("Nick") is available for GC.
thanks
- Himanshu


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
John Spindler
Greenhorn

Joined: Feb 12, 2002
Posts: 28
and that is exactly what I said but was told that it is wrong. Can someone clarify?
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
I have another question, on the same lines...
This one is from JQPlus.
Question ID :987953460230
How may objects will be eligible for GC just before the method returns?
public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}
Choices are -
a) 0
b) 1
c) 2
d) 3
e) 4
-------------
I think the answer should be 2, but JQPlus doesn't seem to agree on the fact that the object referenced by string variable 'str' is eligible for GC... so the answer given there is b) 1
I think we need some help from Valentin/Rob...
Lemme rephrase that, we need Urgent help here... exam's tomorrow...
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
http://www.coderanch.com/t/190163/java-programmer-SCJP/certification/Mock-Exam-gc
"The string (literal) will be created at compile time and exist for (at least) the lifetime of the class (note, not the object) in which it is defined.

Depending on the JVM it may become a candidate for garbage collection if its classloader is detroyed or the actual class definition is unloaded. These are very unusual operations, though, so for all practical purposes it exists for the life of the JVM."


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
"b references a string constant that is in the String Pool before the program is started. Therefore, it is never garbage collected."
http://www.coderanch.com/t/190211/java-programmer-SCJP/certification/Garbage-Collection

Do a search in this forum on "string pool" and you'll come up with all sorts of threads that show that a String literal "abcd" is for all practical purposes never garbage collected.

However, the exam does not cover String pool, so you're better off concentrating on garbage collection of objects other than Strings, or at the very least using String objects created by new String() which are garbage collected just like other objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MindQ #36