I don't understand the answer of two MindQ garbage collection questions. The first is:
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
Answer is 1 object eligible for garbage collection, I selected 2 objects name doesn't refer to any String object, newName refers to "Frieda" String object, so aren't both "Nick" and "Jason" String objects available for garbage collection?
Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object. b) The finalize method is always called before an object is garbage collected. c) Any class that includes a finalize method should invoke its superclass' finalize method. d) Garbage collection behavior is very predictable.
Answer is a, b and c are true, I selected only a and c are true If finalize method has been called once, then the finalize method isn't called again before garbage collection. So isn't option b false?
In Question 2, option a is correct because you can get Singleton object via Runtime.getRuntime() and call method gc() on it.
In Question 1, when newName is assigned string "Jason", string "Nick" is left with no references. Then when newName is assigned variable name, string "Jason" is left with no references. So that's two String objects with no references, "Nick" and "Jason". That is why I thought the answer is 2.
Originally posted by Sachin Kapoor: In Question 1, when newName is assigned string "Jason", string "Nick" is left with no references. Then when newName is assigned variable name, string "Jason" is left with no references. So that's two String objects with no references, "Nick" and "Jason". That is why I thought the answer is 2.
there's your problem. newName is not assigned to variable name. newestName is. [ August 14, 2007: Message edited by: Fred Rosenberger ]
1st Question: newname points to NICK. newname points to JASON....leaving NICK as a orphan... name points to FRIEDA... newestname points to name..i.e., points to FRIEDA. name points to "null".
But: FRIEDA is still being referenced by newestname.. so.. it wont be eligible for GC.
Answer: how many orphans are there...? only one..... "NICK.
Question 2: Explanation: 1: Garbage Collector can be called using a runtime object... you can make a runtime object using...
2: Finalize method is always called before every object is garbage collected. Finalize() method runs at most once for every object, before GC()is called on it,to check whether it is eligible or not.
3: any class that includes a finalize method should invoke its super class's finalize method. every class has some objects and the class also has subclasses. these subclasses will also have some objects... there can also be some objects that have references made to them from their super class... which also had to be checked at runtime. Because if there is/are any such references left, then they will create situations not permissible for garbage collection.... so these references have to be dealt.. so these references are also checked..
4: Garbage Collection behavior is predictable. this as we all know is wrong ... hence falls....
Answers: 1: one object is eligible for GC(). 2: a,b,c are true.
For the first question, I will also go with Manfred. There is no object eligible for garbage collection, since no object has been created in runtime.
For the second question, I think the answers are (a) and (c) (a) I am confused with the language (b) is not correct since, finalize() method runs at most once for every object. So at the second attempt of garbage collection process, finalize() method will not be called.
Joined: Aug 11, 2007
Manfred Klug, thanks for the link on Corey McGlone's article. It's very interesting. After a read to the article, i will say contrary of what i post yesterday that no object is eligible for Garbage collection.
subject: Garbage collection questions in MindQ exam