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

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

Moderator: admin

Post Reply
Vermeulen
Posts: 12
Joined: Wed Jul 15, 2015 4:05 pm
Contact:

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

Post by Vermeulen »

I answered correctly but only because I happened to see something similar before. I think the piece of knowledge required is not the kind of thing you find in a typical Java book, perhaps only in the Java Puzzlers book or Stackoverflow (or of course the JLS).

The explanation could have made it clearer that there is a subtle rule you have to know: in an expression like "s+= expr", the value of s to be added to the value of the expression is the value BEFORE evaluating the expression, not after. In other words, s is assigned the old value of s + the value of the expression. The language designers could have also implemented this otherwise: first evaluate the expression and then add the result to the current value of s. This really makes a difference because the expression contains "++s". If it were implemented this way, the value would be one more.

olograph
Posts: 6
Joined: Mon Feb 01, 2016 3:13 pm
Contact:

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

Post by olograph »

It seems that:

Code: Select all

s = 5;
s += s + mx(s) + ++s; 
Starts to translate to:

Code: Select all

s += 5 + mx(5) + 6; 
But isn't "++s" supposed to precede the other sub-expressions which would make

Code: Select all

s + mx(s) 
become

Code: Select all

6 + mx(6) 
as they are evaluated after?

It seems to be that variable values are resolved all at once at the beginning of the expression... still pretty confusing.

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

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

Post by admin »

olograph wrote: But isn't "++s" supposed to precede the other sub-expressions which would make

Code: Select all

s + mx(s) 
become

Code: Select all

6 + mx(6) 
as they are evaluated after?
No, that is not correct. While parsing the expression, s + mx(s) + ++s, you will start from left to right and substitute the values for the variables as you go forward. Now, when you encounter, ++s, you increment s and then put the new value of s in the expression. In case of s++, you put the existing value of s and then increment s. Therefore, s + mx(s) + ++s will be resolve to : 5 + mx(5) + 6. But you have something like this:
s + mx(s) + s++, then that will resolve to 5 + 5 + m(5).

Important thing to note here is that in both of the following expressions, you will use 6 for the third term:
s + mx(s++) + s : 5 +mx(5) + 6
s + mx(++s) + s : 5 +mx(6) + 6

This is because s has already been incremented while working out the value of the second term i.e. mx(s++) or mx(++s).

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

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post by vlezz94 »

Hi Paul!

Right now I'm a little bit confused with this piece of code:

Code: Select all

static int mx(int s){
               for(int i=0; i<3; i++){
                         s = s + i;
                         }         
          return s;
          }

// and the following code snippet:

          int s = 5;
          s += s + mx(s) + ++s;
          System.out.println(s); 
Specifically with mx method. Because when I ran the program I added a line to the method to see the value of i in the console. And the output is:
  • 0 // i
    1 // i
    2 // i
    24 //Value of s
So, if the last value of i is 2, why when I pass s to the mx method i end up with 8 instead of 7 (5 + 2)?

Maybe is an stupid question but I'm really confused.

Note: I understood everything else in the excercise.

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

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

Post by admin »

You need to evaluate the value of s in mx step by step.
s1. You pass 5 to mx.

Loop starts: s is 5 and i is 0
s2. s = 5+0, s becomes 5 and i becomes 1
s3. s = 5+1, s becomes 6 and i becomes 2
s4. s = 6+2, s becomes 8 and i becomes 3
loop terminates because i is not < 3.

So you can see the method mx returns 8 and not 7.

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

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post by vlezz94 »

Oh, I see it now. Thanks for the explanation Paul

ramini1996
Posts: 1
Joined: Fri Dec 16, 2016 6:05 am
Contact:

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

Post by ramini1996 »

Why is ++s not evaluated first in the expression(s+= s+mx(s)+ ++s) when ++ has higher precedence than +

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

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

Post by admin »

A complete explanation of expression evaluation is not possible in a forum post but there are three things that you need to consider while evaluation of an expression - operator precedence, associativity, and evaluation order. In this case, ++s will be evaluated later because it occurs later in the expression.
You will need to go through a good book to understand this topic.
If you like our products and services, please help us by posting your review here.

mario catanzariti
Posts: 2
Joined: Sat Feb 16, 2019 6:29 am
Contact:

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

Post by mario catanzariti »

Sorry, but but what Ramini says is pertinent, studying is not the problem. In all books it s written that when in an expression there is the pre or post increment operator, it is the same as brackets, so it is necessary to evaluate before, independently from the position. Also Enthuware in another example say like that. In this case for example would be like that: s += s + mx(s) + (++s) . Is there any rule not clear maybe?

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

Post by wdphipps »

Hi team,

Thanks for the discussion on this so far. Could someone explain why the higher precedence is not a factor in this situation?

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

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

Post by admin »

Precedence is a factor but not in the way you are thinking. Precedence determines the operand to which an operator is applied when there are two possibilities. Actual evaluation is always done from left to right. Please go through section 6.1.6 Operator precedence and evaluation of expressions of OCP Java 11 Certification Part 1 Exam Fundmentals by Hanumant Deshmukh book carefully. It explains this point in detail.
If you like our products and services, please help us by posting your review here.

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

Post by wdphipps »

On an additional reading of that section, I suppose the "binding" element that the book discusses comes into play. So even though '++' is of higher precedence, the operator itself is bound to 's', but only once the other operations have evaluated, since it is last from left to right. So the comment above that mentions s++ taking place multiple times does not happen. Would that be an accurate explanation?

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

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

Post by admin »

Yes, that sounds about right.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests