This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC ??" Watch "GC ??" New topic
Author

GC ??

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
After which line will the object created at line XXX be eligible for garbage collection?

Ans
//2
//3
//4
Never
Can't say
ans is Never ... I say Can't say ..as you do not know when the caller(of this method) will release the object


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I think you have to just focus on the code you see, and not other code that you know is there, but not part of the question.
If you just look at the method, it's clear that the object being returned is not eligible for garbage collection ever, in this method.

Rob


Rob
SCJP 1.4
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In my exam I found a question similar to yours. But the "D" answer spells "Never for this method". Maybe who was make your question forgot write "for this method". But remember in the exam select the more closer to the truth or the truth.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
I just wish tha I won't get ambiguous method ..
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Ravish,
Take all practice questions and their answers with a grain of salt. Don't worry about ambiguity in the real exam. The folks at Sun have gone to great lengths to make sure that the questions in the real exam are not ambiguous. As mentioned in RHE:
There are no trick questions on the exam, but every question requires careful thought. The wording of the questions is highly precise; the exam has been reviewed not just by Java experts, but also by language experts whose task was to eliminate any possible ambiguity

(emphasis by jl)


Junilu - [How to Ask Questions] [How to Answer Questions]
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
because:
c = b; //2
the memory location for object assigned to b, hence, when b is assigned to something else, the object that b once held is now in c, correct? I know we went thru one similar to this
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Paul,
that's correct but b still references the object until it is assigned some other object or null.
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Valentin,
Which is in this case correct?
b = a = null
Fair to say that the other object is GC'd:
Object c,d = new Object()
c = b
following this assignment?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Paul Salerno:
Valentin,
Which is in this case correct?
b = a = null
Fair to say that the other object is GC'd:
Object c,d = new Object()
c = b
following this assignment?

The new object referenced by variable d will be eligible for garbage collection when the method ends. You aren't setting d to null anywhere in this method, so in the method, the object referenced by d is never eligible for gc. However, since d is a local variable, it ceases to exist after the method ends, so at that time, the object referenced by d is eligible for gc, since there are now no references to it.
Rob
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Last word on this GC. I'm really have great notes thanks to you all!
so in this case:
Object c = new Object()
c = b
obj referenced by c would be eligible for GC.
however here:
Object c,d = new Object()
c = b
d also references the same object which isnt being set to null anywhere. since d is a local var it will cease to exist following the completion of the method, then that object ref by d is eligible for GC.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Be aware though that in
Object c,d = new Object();//line 1
c=b; //line 2
only d is initialized on line 1 and c is not... c is initialized on the line 2
HIH
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Junilu Lacar:
Ravish,
Take all practice questions and their answers with a grain of salt. Don't worry about ambiguity in the real exam. The folks at Sun have gone to great lengths to make sure that the questions in the real exam are not ambiguous. As mentioned in RHE:

(emphasis by jl)

Hope to catch those precise wordings.....
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Valentin,
I didnt know that, I thought they both were.
-Paul
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Originally posted by Paul Salerno:
Valentin,
I didnt know that, I thought they both were.
-Paul

Try this:

What is the output ?
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
wont compile !
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Roy Ben Ami:
wont compile !

I believe this is Val's point. It shows clearly that the assignment is only happening to variable j.

Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC ??