About Question enthuware.ocajp.i.v7.2.919 :
Moderator: admin
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
public int getObject(Object a) //0
{
Object b = new Object(); //XXX
Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return 0; //4
} // 5
in this code, when is XXX eligible for garbage collection ? after line 5 ?
can you also tell me , once control is abruptly returned back after line 4 back to the caller, when actually is the scope of this method over ?
{
Object b = new Object(); //XXX
Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return 0; //4
} // 5
in this code, when is XXX eligible for garbage collection ? after line 5 ?
can you also tell me , once control is abruptly returned back after line 4 back to the caller, when actually is the scope of this method over ?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
Scope of a method is always from opening bracket to closing bracket.
Object will be eligible to be GCed after line //4 itself. The difference between //4 and //5 is not important for the purpose of the exam.
Object will be eligible to be GCed after line //4 itself. The difference between //4 and //5 is not important for the purpose of the exam.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
after line 4 anyways the control is transferred back to the caller, which means that we are out of the scope of the method after line 4 ,
and hence we say as long as we are in/within the method , the object is never eligible for GC .
Am I right ?
and hence we say as long as we are in/within the method , the object is never eligible for GC .
Am I right ?

-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
That is correct.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
thank you !
and one more clarification .
if instead of return c, if we had return (c=null);
then on this line itself the object would become eligible for garbage collection right ? i.e. after the expression (c=null ) is evaluated . since before the full statement return (c=null); is complete, when (c=null ) is evaluated itself its eligible for GC.
Am I right ?
and one more clarification .
if instead of return c, if we had return (c=null);
then on this line itself the object would become eligible for garbage collection right ? i.e. after the expression (c=null ) is evaluated . since before the full statement return (c=null); is complete, when (c=null ) is evaluated itself its eligible for GC.
Am I right ?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
yes, right.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
based on this explanation (from another question 7.2.921) i should think that object can be garbage collected after "return" statement i.e. after line 4.Once a method returns all the variables that it created are gone. The stack space (where all the automatic variables are kept) is cleared up and reclaimed. So there is no question of a variable hanging on to an object after the method returns.
Where am i wrong?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
The variables of this method are indeed gone after the method returns. But in this case the return statement returns the reference to the object to the caller.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
Why is answer nr. 5 not a correct option?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
Because it can be determined the object can never be garbage collected in this method. Option 4 and 5 are contradictory. So, if 4 is correct, 5 cannot be correct.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
Had there been no Option 4, The Option 5 would be correct ?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
In that case, yes.
-
- Posts: 1
- Joined: Thu Dec 20, 2018 6:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
Hi,I get your point @inaelrodrigues. Honestly,
I chose the same answer.
For me, after line 3, the references a, b and c point to null, so the object itself is eligible for garbage collection from that point (line 3) and not later.
I chose the same answer.
For me, after line 3, the references a, b and c point to null, so the object itself is eligible for garbage collection from that point (line 3) and not later.
-
- Posts: 13
- Joined: Thu May 16, 2024 5:30 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
For this question, it seems to me that both answers 4 and 5 are correct. In other words, the object will never be eligible for garbage collection in this method, and also we cannot determine when it will be eligible for garbage collection based on the information provided. It'll surely be somewhere outside this method when the object stops being referenced, but we can't determine when that will be.
How can I choose between these two answers when I take the exam?
How can I choose between these two answers when I take the exam?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.919 :
If you have to select one option then //4 is correct based on the information is given. "Cannot be determined" is in the context of the given code. The question does not ask you to assume anything about what is happening outside of the given code and so that should not be considered for selecting the correct answer.
Who is online
Users browsing this forum: Google [Bot] and 31 guests