• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Garbage collection questions in MindQ exam

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
He's giving us the slip! Quick! Grab this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic