About Question enthuware.ocajp.i.v8.2.944 :

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

Moderator: admin

Post Reply
alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

About Question enthuware.ocajp.i.v8.2.944 :

Post by alkour »

Hi,

I think, there is some small typo in the explanation to A answer.

For example, the following is valid: int b = 0, c = 0;
int a = b = c = 100;

Even the following is valid:
int b , c; // Not initializing b and c here.
int a = b = c = 100; // declaring a and initializing c, b, and a at the same time.
Meanwhile
int a = b = c = 100;
is not valid.

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

It is talking about 2 lines of code in each example. Not about individual lines. That is why it says, "The following is valid" and not "The following are valid".

The line int a = b = c = 100; on its own is not valid. But both the lines together are valid. That is what the comments are trying to explain -
int b , c; // Not initializing b and c here.
int a = b = c = 100; // declaring a and initializing c, b, and a at the same time.

The second line is valid if the first line exists.

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

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by alkour »

Thanks a lot.
I have got the point.

Deleted User 3513

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by Deleted User 3513 »

Java does not allow chained initialization in declaration so option 1 and 5 are not valid.
Is this applicable anywhere in the program? I tried doing int a=b=c=100; in methods, classes, initializers, etc. and it returns an error something like it cannot find symbol. Is there an exception to this rule?

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

This rule is about chained initialization combined with declaration. i.e. where you declare as well as initialize at the same time. For example, int a = b = c =10; Here you are trying to declare a, b, c and initialize also.
But this is valid:
int a = 0, b=0, c = 0; //This is valid because it is not chained.
a = b = c = 10; //This is valid because although it is chained but there is no declaration, only initialization.
If you like our products and services, please help us by posting your review here.

yassine
Posts: 8
Joined: Thu Dec 07, 2017 4:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by yassine »

in previous questions it is said that "java evaluate operation from left to right" .taking that into account .I thought that :for this operation :
int b = 0, c = 0;
int a = b = c = 100;
System.out.println(a+" "+b+" "+c)
will be executed as :
a=b, then b=c, finally c=100;
which will print :
0 0 100
why is that is not true ?

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

You need to keep 2 things in mind while evaluating expressions - evaluation order and operator precedence. When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Now, = has least precedence and that is why, the second operand i.e. b = c = 100; will be evaluated before executing the assignment operator.

The same logic applies to b = c = 100;
If you like our products and services, please help us by posting your review here.

yassine
Posts: 8
Joined: Thu Dec 07, 2017 4:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by yassine »

thank you for responding, that's make sense

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

Sorry, I misspoke above. The reason why a = b = c = 100; is evaluated as a = ( b = (c = 100) ); is because of "associativity" and not evaluation order. JLS section 15.26 clearly says that the = operator is right associative right-associative (they group right-to-left). Thus, a=b=c means a=(b=c) , which assigns the value of c to b and then assigns the value of b to a .

HTH,
Paul.
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.v8.2.944 :

Post by flex567 »

>When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Why do we evaluate from left to right?

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

flex567 wrote:
Sat Jun 23, 2018 4:45 pm
>When you evaluate from left to right, the first operand of the first = is a and the second operand is b = c = 100;
Why do we evaluate from left to right?
Rule of Java language. Which book are you following? It should have explained this in the Operators and Evaluation chapter.
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.v8.2.944 :

Post by flex567 »

I mainly use OCA Oracle Certified Associate Java SE 8 [2014]

From the book(p.52):
>Unless overridden with parentheses, Java operators follow order of operation, listed in Table 2.1, by decreasing order of operator precedence. If two operators have the same level of precedence, then Java guarantees left-to-right evaluation.

In our case the statement contains operators with the same level of precedence because of that we have left-to-right evaluation?

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

Order of evaluation is always left to right. There are three thing that determine how an expression is evaluated - Precedence, Order of evaluation, and associativity. All three are different. It is not possible to explain them in a post here but google them to know more.
If you like our products and services, please help us by posting your review here.

SL____
Posts: 3
Joined: Fri Jul 26, 2019 1:12 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by SL____ »

I'm sorry, but regarding first one i don't get it.
"Which of the following are valid code snippets appearing in a method:"
I suppose
1) each option is individual
2) since it is a code snippets inside a method, around this code and around this method could be any code. I would undestand it has to be out of String literal, but why should i think no other a and b ints declaration must be?

Thats how i read the question:
"Which lines of code inserted individually in a method could be valid?"

What did i miss?

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

You interpreted correctly.
Are you able to put the code given in option 1 in a method and compile?
If you like our products and services, please help us by posting your review here.

SL____
Posts: 3
Joined: Fri Jul 26, 2019 1:12 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by SL____ »

Yes, it was discussed earlier, given "a" and "b" declared already (grey zone context) i'm able to compile.

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

Re: About Question enthuware.ocajp.i.v8.2.944 :

Post by admin »

It is a possible interpretation but from exam perspective, not a valid one. Specially when option 2, which shows the possibility that you are talking about, is present.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: gadsgadsx and 107 guests