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

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

Moderator: admin

Post Reply
Danail

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

Post by Danail »

Hello,

In expression (b2 != b1 = !b2) first is evaluated !b2, but in "Explanation" section you said that first "b2 != b1" is evaluated.

The whole order of evaluation is:
1. !b2 -> true
2. b2 != b1 -> false
3. false = true -> compile time error

Please, correct me if I am wrong.

Danail

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

Post by Danail »

I don't know why according to http://docs.oracle.com/javase/tutorial/ ... ators.html "Operator Precedence":
unary operator "!" has more priority than equality operator "!=" ?
Danail wrote:Hello,

In expression (b2 != b1 = !b2) first is evaluated !b2, but in "Explanation" section you said that first "b2 != b1" is evaluated.

The whole order of evaluation is:
1. !b2 -> true
2. b2 != b1 -> false
3. false = true -> compile time error

Please, correct me if I am wrong.

baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

! has higher precedence than !=

openmic62
Posts: 5
Joined: Wed May 01, 2013 5:46 am
Contact:

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

Post by openmic62 »

When I took the test I answered "Compile time error", but got flagged for an incorrect answer. So, I copied the code from the test into a main method of a class I created.

Code: Select all

class T2Q04_QID0949{
	public static void main(String[] args){
		boolean b1 = false;
		boolean b2 = false;
		if (b2 != b1 = !b2){
			System.out.println("true");
		}else{
			System.out.println("false");
		}
	}
}
I did indeed get a compiler error.

Code: Select all

T:\src\main\java>javac -d ..\..\..\target\classes  T2Q04_QID0949.java
T2Q04_QID0949.java:10: error: unexpected type
                if (b2 != b1 = !b2){
                       ^
  required: variable
  found:    value
1 error

T:\src\main\java>javac -version
javac 1.7.0_07
I think Enthuware needs to fix this question, unless they can prove me wrong.
The correct answer is: Compile time error

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

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

Post by admin »

I see that "Compile time error" is indeed set as the correct answer.
If you like our products and services, please help us by posting your review here.

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

Yes, I wanted to see what the forum had to say about this question. The explanation does not mention anything about how the ! unary operator has more precedence that the equality != or the assignment = operator.

So it seems like:
  • 1.

    Code: Select all

    !b2
    is evaluated and returns true
    2.

    Code: Select all

    b2 != b1
    is evaluated which evaluates to true != false which returns true

    3. Then the literal boolean value true is tried to be used like a variable container to take the value of the expression

    Code: Select all

    !b2
    but it cannot do this as a literal is not a variable so the code fails and results in a compile time error
What does the enthuware hive mind think?

Regards,

javanaut

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

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

Post by admin »

Yes, unary operator has more precedence. But even with that logic, the answer will not be any different.

(b2 != b1 = !b2) =>
(b2 != b1 = true) => (!= has more precendence than =)
(false = true) Which is illegal

Remember that there will actually be no execution because of compilation failure. We are just trying to hypothetically execute the expression and see whether it boils down to an illegal situation or not. Here, we see that it does resolve to an illegal expression and so it will not compile.

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

javanaut
Posts: 22
Joined: Wed Aug 21, 2013 12:29 am
Contact:

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

Post by javanaut »

Cool. :D

And yes, I guess the compiler goes through this logic in nanoseconds to see that it will not compile. Thank-you for the reply Paul.

Regards,

Javanaut

agupta108
Posts: 4
Joined: Sat Jan 20, 2024 9:26 am
Contact:

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

Post by agupta108 »

Answer is correct, but explaination is a little incorrect in Enthuware:
"!b2 -> true" will be executed first,
and then "b2 != b1 -> false" will be executed,
resulting "false = true" in a Compile Time Error

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 201 guests