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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

For better understanding the explanation:
If we eliminate "w = new Wrapper();" from changeWrapper method, would the last answer be "59"?
tnx

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

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

Post by admin »

Please try it out and if you have trouble understanding the output, you may post it here.
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

admin wrote:Please try it out and if you have trouble understanding the output, you may post it here.
after change
static Wrapper changeWrapper(Wrapper w){
//w = new Wrapper();
w.w += 9;
return w;
}
the code return 59 and 68. I got it.
tnx

dondon1903
Posts: 2
Joined: Wed Mar 12, 2014 12:17 pm
Contact:

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

Post by dondon1903 »

I haven't come across 'nested classes' in my course. Is it likely that there will be questions on this in the exam?

If so, would anything more than a basic knowledge be required?

Regards,
Scott

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

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

Post by admin »

No, it is very unlikely to get a question on nested classes. (While Wrapper is a nested class but this question is not really about nested classes. I have updated the problem statement to move Wrapper class outside TestClass to avoid this complication altogether.)

We do have a few questions that are related to nested classes because some candidates have reported getting a question on it. We have also marked such questions clearly with a statement saying, "This question may be considered too advanced for this exam" so that a user may ignore the question if he/she is short on time.

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

dondon1903
Posts: 2
Joined: Wed Mar 12, 2014 12:17 pm
Contact:

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

Post by dondon1903 »

Thanks ..That's a bit of a relief.

Regards,
Scott

stephenchau
Posts: 1
Joined: Mon May 26, 2014 12:30 am
Contact:

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

Post by stephenchau »

I don't understand that as the return type of method changeWrapper is Wrapper, so the correct calling expression should be:

w = changeWrapper(w);

but why below expression is legal as well?

changeWrapper(w);

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

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

Post by admin »

Return value is simply ignored.
If you like our products and services, please help us by posting your review here.

JamesB
Posts: 1
Joined: Wed Aug 02, 2017 4:03 pm
Contact:

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

Post by JamesB »

static Wrapper changeWrapper(Wrapper w){
w = new Wrapper();
w.w += 9;
return w;
}

So in the first call to this method:
changeWrapper(w);

The statement that calls the method doesn't handle the return value so
the increase by 9 is applied to the Wrapper object passed to the method and
not the Wrapper object local variable?

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

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

Post by admin »

JamesB wrote:static Wrapper changeWrapper(Wrapper w){
w = new Wrapper();
w.w += 9;
return w;
}

So in the first call to this method:
changeWrapper(w);

The statement that calls the method doesn't handle the return value so
the increase by 9 is applied to the Wrapper object passed to the method and
not the Wrapper object local variable?
w.w += 9 is applied to the wrapper object referred to by the variable w. This object is a new Wrapper object created just before this line and is different from the one passed to this method in the argument.
The method then returns this new Wrapper object. The original Wrapper object that was passed to this method is not changed.

This has nothing to do with what happens to the return value after this method returns. In the first call to changeWrapper(w), the return value is ignored. So this object that was created in the chargeWrapper method is lost and garbage collected later.

In the second call, the object returned by changeWrapper is assigned back to variable w. So the original object that was referred to by w before passing to the method is lost.

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

johanna
Posts: 2
Joined: Fri May 27, 2022 5:15 am
Contact:

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

Post by johanna »

Code: Select all

class Wrapper{
        int w = 10;
}

public class TestClass{
    
    static Wrapper changeWrapper(Wrapper w){
        w = new Wrapper();
        w.w += 9;
        return w;
    }
        
    
    public static void main(String[] args){
        Wrapper w = new Wrapper();
        w.w = 20;
        changeWrapper(w);
        w.w += 30;
        System.out.println(w.w);
        w = changeWrapper(w);
        System.out.println(w.w);
     }
}
This is my understanding of the first part, please correct me if something's not right: in the main method, firstly, there is an instance of class Wrapper created. Then, it is used to access instance variable 'w' from class Wrapper, and change it's value to 20. Next, the the reference variable 'w' is passed into the changeWrapper() method expecting a Wrapper reference as an argument, but the result is ignored, because it's not assigned to anything. w.w += 30; refers to the instance variable in class Wrapper (it's value has been changed to 20) so, it's value becomes 50, which is printed.

w = changeWrapper(w); is the line I don't understand.
1. Since the method changeWrapper() returns a numeric value, how can that be assigned to an instance of class Wrapper?
2. How is w = new Wrapper(); in the changeWrapper() method different from the argument the method takes? Isn't this line simply assigning the argument Wrapper w to point to an object of class Wrapper?
3. Since there is an instance of class Wrapper defined in the method, why doesn't it access the instance variable of class Wrapper when this line is executed w.w += 9; ?

Thank you for your help in advance.
Johanna
Last edited by admin on Fri May 27, 2022 7:01 am, edited 1 time in total.
Reason: Added [code] [/code] tags for code.

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

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

Post by admin »

johanna wrote:
Fri May 27, 2022 5:36 am
w = changeWrapper(w); is the line I don't understand.
1. Since the method changeWrapper() returns a numeric value, how can that be assigned to an instance of class Wrapper?
If you notice the signature of changeWrapper method ( static Wrapper changeWrapper(Wrapper w) ), it is returning a Wrapper and not a numeric value. Observe the return statement in the changeWrapper as well. "return w" implies that it is returning the instance referred to by the local variable w. So, there is no issue in assigning the return value of this method to the variable w of main method.
2. How is w = new Wrapper(); in the changeWrapper() method different from the argument the method takes? Isn't this line simply assigning the argument Wrapper w to point to an object of class Wrapper?
new Wrapper() is a call to the constructor. (This constructor is not visible in the code but it is there. It is provided by the compiler. Read about "default constructors".) So, when you do new Wrapper(), you are creating an entirely new instance of the Wrapper class. This instance is different from the instance that was passed to the changeWrapper(Wrapper w) method as an argument. When the changeWrapper method starts executing, the method parameter w is pointing to the Wrapper instance that was passed as an argument by the main method. But as soon as the method executes the statement w = new Wrapper();, variable w starts pointing to the new Wrapper instance. The variable w of the main method still keeps pointing to the same old wrapper object.
3. Since there is an instance of class Wrapper defined in the method, why doesn't it access the instance variable of class Wrapper when this line is executed w.w += 9; ?
It does access the instance variable w. But the instance whose instance variable w is being updated is different from the one that was passed to the changeWrapper method.

Think of it like this:

I take a notebook, tie a thread to that notebook, and throw it over to you (the Wrapper instance passed in the argument by the main method to the changeWrapper method). I expect you to write something on it and when I pull the thread back, I expect to read what you wrote.

Now, instead of writing in the notebook that I threw over to you, you get some other other new notebook (w = new Wrapper() ) and write in that notebook. And then you throw that new notebook back to me (return w). But I am ignoring the notebook that you threw back to me (the return value is not used). I am pulling the thread to retrieve the original notebook (the main method is using the same reference that it passed to the changeWrapper method). And that notebook is unchanged.

I suggest you go through the first chapter of OCP Java Fundamentals book (specifically, section 1.4). That will clear all your doubts. You don't have to buy the book. You can read this part in book preview.

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

johanna
Posts: 2
Joined: Fri May 27, 2022 5:15 am
Contact:

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

Post by johanna »

Thank you for the explanation, it's completely understandable now.

Regards,
Johanna

Post Reply

Who is online

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