aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6 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 "Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6" Watch "Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6" New topic
Author

Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6

Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Q10.
Given:

Which two are true about the objects created within main(), and eligible for garbage collection
when line 14 is reached?

ANS : Five objects were created; Two objects are eligible for GC
HOW?

Q.11
Given:

When line 16 is reached, how many objects will be eligible for garbage collection?

ANS : 1
HOW?

MCSA 2003 | Preparing For OCPJP/SCJP6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Q.10. 4 objects are created ; references are dz, da, da[0], d
da[1] and d are pointing to the same object only , so when you assign null to the references , onlyy one object is eligible for GC.

Q.11. no object is eligible for GC
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For Q10, five objects are created, let's count!
One in the 8th line, array object.
Two in the 9th line, the Dozens object,
Three - as the instance object of the Dozens object which you've created in the 9th line,
Four - in the 10th line,
Five - as the instance object of the Dozens object, which you've created in the 10th line.

Now, the object created in the 10th line has two reference variable, d and da[1]. But you make them null in the 12th and 13th lines, so that object and its instance variable object dz are eligible for GC!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For Q11,
When you make a1 as null in 15th line, the object you've created in line 11 is eligible for GC, and since the static object b1 of that Alpha class, is not eligible for GC. Because the object has a reference b1, which you've created in the 10th line, and the instance variable b2 of the Alpha class is already null, so no object is eligible for GC. So almost, 1 object is eligible for GC!
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
For Q 11, see the attached image,
black lines show the links to objects as they were created.
Red crosses show the nulled references
Magenta lines show the access path of objects.
In this process, only the object a1 is lost, all other objects are available



[Thumbnail for Q 11.JPG]



Cheers,
Divyesh.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I am totally wrong in my previous post.

Abimaran given the fix!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Seetharaman Venkatasamy wrote:I am totally wrong in my previous post.

Abimaran given the fix!


It's just a careless mistake, happened to everyone!
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Thanks All,
Sorry, but Q.11 is not cleared yet.....
please describe it more.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Vishal Kashyap wrote:Q.11 is not cleared yet.....


well, I will try to explain to you


and

though Alphas static/class variable points to the Beta Object(b1) , the variable is not in the instance/object memory a1. class variable store in static storage area of
Class object. so when you say al = null; the object referenced by a1 is eligible to GC. *but Beta b1 = new Beta();* this object is refernced by Alpha's class variable[b1], so even though you assign null to b1 reference in main[b1=null], the Object(Beta b1 = new Beta()) is not eligible to GC.


is that make sense?

Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Dear Seetharaman,

if any reference variable is pointing to NULL then is it eligible for GC?
if yes,
then there are three reference variable here pointing to NULL; what will happen?
otherwise
will you please clear me that when any reference variable would be eligible for GC.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vishal Kashyap wrote:Dear Seetharaman,

if any reference variable is pointing to NULL then is it eligible for GC?
if yes,
then there are three reference variable here pointing to NULL; what will happen?
otherwise
will you please clear me that when any reference variable would be eligible for GC.


You are wrong here! Garbage collection is for objects, not for reference variables or anything! If a variable not referring to any objects, then no point of talking aboult GC on that variable!
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Thanks Abimaran Sir,
Now, this misunderstanding has been cleared.
Thanks once again.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are Welcome!
Sarvarth Bhatnagar
Greenhorn

Joined: Jan 16, 2013
Posts: 9
Hi, For Qn. 10. I hope this diagram explains the scenario.



[Thumbnail for Ans11_justification.JPG]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6