File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam lab question 42 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 "Exam lab question 42" Watch "Exam lab question 42" New topic
Author

Exam lab question 42

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



How many objects are eligible for garbage collection ?

i think answer should be 1

bc--> new Gabc which contains 2 references of type Gabc
hence,
bc.ob[0]---->new Gabc //line1
bc.ob[1]---->new gabc(let this be D)

D contains 2 refernces of type Gabc
hence,
d.ob[0]---->new Gabc
d.ob[1]---->bc.ob[0] //line2

so,when bc.ob[0]=null;

then line 1 would be equivalent to
null----> new Gabc

then line 2 would be equivalent to
d.ob[1]---->null

so,object at line 1 would be eligible for GC





OCPJP 6.0 93%
OCPJWCD 5.0 98%
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
The answer should be zero.

When the following code is run:

the reference to newly created object is changed to null.

But the object is not eligible for Garbage Collection.
It can be reached through bc.ob[1].ob[1] as that action is performed in previous line:


OCPJP6
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Trivikram :
the object is not eligible for Garbage Collection.
It can be reached through bc.ob[1].ob[1] as that action is performed in previous line:

bc.ob[0]---->new Gabc()

and
bc.ob[1].ob[1]----> bc.ob[0]


so,after bc.ob[0]---> null


bc.ob[1].ob[1]---->null


so,object at line 1 would be eligible for GC


e.g
if
a---> b--->c
and
b--->null
then a ----> null too
as refers to b
(here a, b are references and c is object)
so c would be GC candidate ..
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
e.g
if
a---> b--->c
and
b--->null
then a ----> null too
as refers to b
(here a, b are references and c is object)


It's incorrect.
When you say a---> b--->c
It's a ---> c
and also b ---> c

when b = null, it just affects b ---> c, it doesn't affect the reference a
So, a ---> c still holds true
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
If you still haven't understood, try running the following code and go through the output:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exam lab question 42