wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q. on garbage collection 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 "q. on garbage collection" Watch "q. on garbage collection" New topic

q. on garbage collection

Toms Liepins

Joined: Nov 02, 2004
Posts: 21

1. public class NewGarb {
2. public static Object getIt() {
3. Object rg = new Integer(3);
4. Object dg[][] = new Object[1][2];
5. dg[0][1] = rg;
6. dg[0][0] = rg;
7. rg = null;
8. return rg;
9. }
10. }

Which statement is true?
A) The NewGarb class will not compile.
B) The getIt() method must not be declared as static.
C) The NewGarb class compiles, but an exception is received because dg is not set to null.
D) The rg object is eligible for garbage collection after a call to the getIt() method has returned.

The correct answer is D. My question: if array dg was returned instead of Object rg, would that make any difference? I opted for answer B, partly because I was confused with MUST/MAY wording of the answer, and partly because i thought that dg still holds a reference to rg...
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Points in your question:
I think question is formatted in wrong way. Becaues, rg is not object. It is a variable of type Object. So, i do not agree variable is garbage collected. If you make reference of variable rg to null, then object created at line no. 3 will be eligible for garbage collection.

MUSTin point.B :- means, if you include static, then program may fail to compile or runtime exception, or similar type of problem and if you remove static, then this probelm should be eleminated. So B is not correct either.

"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Atul Chandran

Joined: Oct 24, 2004
Posts: 22
If dg is returned and is assigned to a two dimensional array in the method which called getIt() then the object will not be eligible for garbage collection since two references will be reffering to the object.
Toms Liepins

Joined: Nov 02, 2004
Posts: 21
Nitin, Atul, thank you for your explanation!
I agree. Here's the link: http://aspose.com/file-tools
subject: q. on garbage collection
It's not a secret anymore!