• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan's exam

 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question 17
class A {
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Runtime error
j. Compiler error
k. None of the above


I thought the answer was h because && has higher precedence than || according to the table on this webpage http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
But Dan's solutions say the answer is e.
Is this a mistake? If not can someone please explain it to me?
Thanks
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things:
1- it's like saying
if (a=true) OR( (b = true) AND (c = true) )

2-Short circuit operator (||). You can forget about the right side, after the OR operator, since the first is true, not evaluating the rest.
Hope it helps
[ July 10, 2003: Message edited by: Andres Gonzalez ]
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1- it's like saying
if (a=true) OR( (b = true) AND (c = true) )


Why can you say this? The way I see it && has precedence over || and thus should be evaluated before ||.
I understand that the way your wrote it would only evaluate a=true because of the short circuit, but since it was not written this way in the question why would you decide to group the expressions as you wrote it?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer to page 216 of KnB book:
if ((x>3) && (y<2) | doStuff())


if (x>3)and either (y<2) or the result of doStuff() is true, then....


does that make sense?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to mention. evaluations are from left to right as well.
cheers
[ July 10, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Damien Howard:

The way I see it && has precedence over || and thus should be evaluated before ||.


Hi Damien,
I think thats the very reason why the expression has to be grouped that way.
This expression
boolean b = a || c && d;
is equivalent to this:
boolean b = a || (c && d);
Much like this expression
int a = c + d*e;
is equivalent to this
int a = c + (d*e);
Hope that helps.
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This expression
boolean b = a || c && d;
is equivalent to this:
boolean b = a || (c && d);
Much like this expression
int a = c + d*e;
is equivalent to this
int a = c + (d*e);
Hope that helps.


Exactly and in the above case you evaluate * first because it has precedence, so following the same logic b&&c should be evaluated first in my opinion.
As to the example Andres referred to in KNB's book pg 216, that is a different example, because | has precedence over && or ||.
precedence order is &, ^, |, &&, ||
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Damien,
Follow this link and go to Marlene's post. She got a very good explanation of what operator precedence means.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Damien
I am not sure about my answer but just trying.
I think whats happening is that a which is true is evaluated first becasue b and c are || operands.
So you can consider this as a || (b && c). Now the term (b && c) is also acting as the second operand for the ||. Now if a is false then the second operand of || would be evaluated but that is not the case. So (b && c) is not being evaluated. So it appears that the precedence is not being followed.
[ July 10, 2003: Message edited by: Anupam Sinha ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Operator precedence is about how to add extra parentheses, *not* about the order of evaluation.


excellent link...
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Alton. That link was helpful
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
poor (Antonym) link.
btw Antonym isn't cast.. ha
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic