Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection questions in MindQ exam

 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

------------------------------------------------------

The second question is this:

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?
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of the second question only c is true, because you can only suggest a garbage collection but not invoke it explicitly. But I'm eager to learn..

For question 1 read carefully again when newestName and newName is used.
 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
three objects are created, Strings with the three names. going through the code...

name points to nothing
newName points to "Nick"
newName points to "Jason", leaving "Nick" with no references
name points to "Frieda".
newestName points to "Frieda".
name points to nothing

at this point, newName points to Jason, and newestName points to Frieda.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 1
name ---> null
Line 2
newName ---> @Nick
Line 3
newName ----> @Jason // Nick is now eligible
Line 4
name ---> @frieda
Line 5
newestname --->@Frida

Only one element is eligible.
 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course! Thanks. It is gonna suck if I make such mistakes in the exam.

Originally posted by Fred Rosenberger:

there's your problem. newName is not assigned to variable name. newestName is.

[ August 14, 2007: Message edited by: Fred Rosenberger ]
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sachin Kapoor:
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
In this sample there is no object eligible for garbage collection. The code uses only string literals, and the objects for those are created at class loading time.

For details have a look at Strings, Literally.
 
Sheelpa Gupta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am still not sure whether calling gc() on Runtime.getRuntime() will invoke garbage collection. As far as I know, it just suggests the system that the garbage collection code should be run.
 
Fedry Kemilau
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, in K&B book pg 246, it is mentioned that exam focus on garbage collection for non String object. Peace
 
sandeep atluri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...

as per your question1:

the answer is 1 object is eligible for Gc..

as per Question2:
a,b,c are true...

Explanation:

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.

Satisfied....

now

Question 2:
Explanation:
1: Garbage Collector can be called using a runtime object... you can
make a runtime object using...


true

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.

true

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..

true

4: Garbage Collection behavior is predictable.
this as we all know is wrong ... hence falls....

False.

Answers:
1: one object is eligible for GC().
2: a,b,c are true.
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic