About Question enthuware.ocpjp.v11.2.3616 :

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

Moderator: admin

Post Reply
ravi2519
Posts: 1
Joined: Sat Jan 16, 2021 9:24 am
Contact:

About Question enthuware.ocpjp.v11.2.3616 :

Post by ravi2519 »

The Buddy object created at line number 2 is set into instance variable upper at line number 3.
Now the object that will be returned from following constructor will remain till the program ends.

Code: Select all

public Buddy(String name, Buddy upper)
So i am confused as to why and how this option is correct:

Code: Select all

Object created at line //1 will be eligible for garbage collection after line //2
And this option incorrect:

Code: Select all

Object created at line //1 will be eligible for garbage collection after line //5

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

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by admin »

>number 2 is set into instance variable upper at line number 3.
No, at //2 the number variable refers to the method parameter and not the instance variable.
This has now been added to the explanation.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

pblanchardie
Posts: 2
Joined: Tue Aug 10, 2021 2:08 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by pblanchardie »

There is a typo, the variable name is upper (instead of number)

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

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by admin »

You are right. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

lucash9
Posts: 4
Joined: Fri Nov 04, 2022 1:51 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by lucash9 »

I think that should be

Code: Select all

b = upper; //2
instead of

Code: Select all

upper = b; //2
to to satisfy the correctness of first answer.

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

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by admin »

No, it is correct. At //2, when you set upper = b, the reference to the Buddy object created at //1 is not stored in the instance variable upper. It is stored in the method parameter named upper, which is lost after the method returns (i.e. after //2 ).
If you like our products and services, please help us by posting your review here.

lucash9
Posts: 4
Joined: Fri Nov 04, 2022 1:51 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by lucash9 »

I'm still not convinced. upper = b; what it actually does is set upper reference at the same as b which is Buddy object. Now we have two references to Buddy: b and upper. No reference to Buddy is lost. Take a look at this code executed at https://www.jdoodle.com/online-java-compiler/

Code: Select all

public class Buddy {
    public static void main(String args[]) {
        Buddy b1 = new Buddy("A"); // 3
		Buddy b2 = new Buddy("B", b1); // 4
		//System.out.println(b1); // 5
    }
    
   	Buddy upper;
	String name;

	public Buddy() {}

	public Buddy(String name) {
		this.name = name;
	}

	public Buddy(String name, Buddy upper){        
		this.name =  name;         
		Buddy b = new Buddy(upper.name);//1
		System.out.println("b reference before line 2: "+b);
 		upper = b; //2
        System.out.println("b reference after line 2: "+b);
        System.out.println("upper reference: " +upper);
	}
}
As a result we will see something like this
b reference before line 2: Buddy@3b192d32
b reference after line 2: Buddy@3b192d32
upper reference: Buddy@3b192d32
which indicates that b still referenced to Buddy.

lucash9
Posts: 4
Joined: Fri Nov 04, 2022 1:51 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by lucash9 »

Maybe you mean a situation after method is called so that local Buddy pointed by b and upper is not stored anywhere so it will be eligible for GC?

lucash9
Posts: 4
Joined: Fri Nov 04, 2022 1:51 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by lucash9 »

It certainly is. Silly me.

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

Re: About Question enthuware.ocpjp.v11.2.3616 :

Post by admin »

Right, b and upper will both reference to the same Buddy object that was created at //1. But after line //2, both of these references are not used and so this object will be eligible for gc after that line because the method ends there.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 42 guests