About Question enthuware.ocajp.i.v7.2.919 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

inaelrodrigues
Posts: 1
Joined: Fri Feb 27, 2015 7:03 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by inaelrodrigues »

After which line will the object created at line XXX be eligible for garbage collection?

Code: Select all

public Object getObject(Object a) //0    {
  Object b = new Object();  //XXX  
  Object c, d = new Object(); //1 
  c = b; //2 b = a = null; //3 
  return c; //4 
}

Why in line "b = a = null; //3" is note marked for garbage collection?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

I am sorry, I did not understand your question. Can you please rephrase?

thank you,
Paul.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by Roibeard »

After which line will the object created at line XXX be eligible for garbage collection?
After (not on) line 4 is when the method is closed. Isn't that what releases resources for collection?


//thanks

GuillaumeBailly
Posts: 3
Joined: Sat Jan 16, 2016 1:20 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by GuillaumeBailly »

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.

Regards,
Guillaume

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

Well, the object will be available for GC only after the assignment b = a = null; is executed not before. If you say, at line //3, it could also mean just before b = a = null; because that is also technically line marked //3.

The point is, the whole statement b = a = null; is at line //3 so it would be incorrect to say that the object will be eligible for gc at that line. Only thing that is certain is that right after line //3, it will be eligible for GC.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

GuillaumeBailly
Posts: 3
Joined: Sat Jan 16, 2016 1:20 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by GuillaumeBailly »

Hi Paul,

Ok, thanks for your answer. Since the question says "After which line...", if I say "line3" I suppose I'm correct too.

Regards,
Guillaume

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

Sorry, I was looking at the wrong code. I checked this question again and the given answer I.e. "never in this method", is correct. Please go through the given explanation.
If you like our products and services, please help us by posting your review here.

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by lenalena »

GuillaumeBailly wrote: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.

Regards,
Guillaume
c doesn't point to null, it points to the object created in line XXX.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by Kevin30 »

Hi Paul,

From the question:
Object c, d = new Object(); // 1
Can you tell me if this line creates 2 different objects (with c and d pointing to two seperate objects) or does this create just 1 object (with c and d pointing to the same object).

I think it creates 2 different objects and not 1. Am I correct?

Thank you in advance!

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

It creates only one object. Further, only d points to that object and not c. c is null.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by shambhavi »

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 ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by shambhavi »

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 ? :)

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by shambhavi »

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 ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

yes, right.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by shambhavi »

thanks ! :joy:

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by Sergey »

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.
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.
Where am i wrong?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by flex567 »

Why is answer nr. 5 not a correct option?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by flex567 »

Had there been no Option 4, The Option 5 would be correct ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by admin »

In that case, yes.
If you like our products and services, please help us by posting your review here.

ethanaidenies
Posts: 1
Joined: Thu Dec 20, 2018 6:54 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.919 :

Post by ethanaidenies »

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.

Post Reply

Who is online

Users browsing this forum: gadsgadsx and 28 guests