wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection questions in MindQ exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection questions in MindQ exam" Watch "Garbage collection questions in MindQ exam" New topic
Author

Garbage collection questions in MindQ exam

Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
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

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

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

Joined: Aug 11, 2007
Posts: 259
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.


SCJP 5.0 | SCWCD 1.4
Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 14, 2007
Posts: 72


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.


SCJP 5 <br />SCWCD <br />Next: ?
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
Btw, in K&B book pg 246, it is mentioned that exam focus on garbage collection for non String object. Peace


Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
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.


Sandeep Atluri
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

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.


MooN
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
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 !!!
 
Consider Paul's rocket mass heater.
 
subject: Garbage collection questions in MindQ exam
 
Similar Threads
MindQ #36
Garbage from MindQ
MIndQ #36
MindQ q#36
garbage collection