aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from the SCJP 1.4 masterexam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from the SCJP 1.4 masterexam" Watch "Question from the SCJP 1.4 masterexam" New topic
Author

Question from the SCJP 1.4 masterexam

Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
So the KandB 1.4 book comes with some testing software.


Can I post a question verbatim from it on here?


Scooby Snacks for everyone...<br /> <br />SCJA, SCJP 1.4
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
yup


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
---------
public class Test{
public static void main(String[] args}{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;

if(b1 & b2 | b2 | b3 & b2) //if1 statement
System.out.print("pig");

if(b1 & b2 | b2 & b3 | b2 |b1) //if2 statement
System.out.println("pen");

}//end main
}//end Test
------------

here's what the answer or reference button provides:

"

The answer is "pen". That's the output. The 'reference button'/tip, says "pig" is not output because it doesn't resolve to true.
\

if(b1 & b2 | b2 | b3 & b2) //if1 statement

So this is more like : (b1 & b2) = false, and next due to order of precednce (b3 & b2), then what??? the statements ends because that was the last of the comparisons within the if statement parentheses, or does it next evaluate OR (|) b2 (the middle b2) as = false as well, which is why it's false?

Basically, does the b2 in the middle get evaluated by itself as an | after the two &s are evaluated? Or (no pun intended), does the evaluation close with the last b3&b2 evaluation?

If the middle b2 is being evaluated lastly, is it looked at like from left to right...for instance ((b3&b2 = false) | b2)=false? Or like a lone ranger, not tied to the pre or post & comparisons? Or, if the middle b2 is evaluated, is it evaluted as b2|b2 (first and second b2's, then b2|b3 (second b2 and last b3)?
[ June 01, 2007: Message edited by: Michael Raymond Jr. ]
Anthony O'Dea
Greenhorn

Joined: Jan 18, 2007
Posts: 8
Michael

Logical AND (&) has higher precedence than logical OR (|).

So (b1 & b2 | b2 | b3 & b2) becomes (false | b2 | false)
and (b1 & b2 | b2 & b3 | b2 | b1) becomes (false | false | b2 | b1)


Anthony O'Dea<br />BSc, PhD, DipIT, pre-SCJP
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
Originally posted by Anthony O'Dea:
Michael

Logical AND (&) has higher precedence than logical OR (|).

So (b1 & b2 | b2 | b3 & b2) becomes (false | b2 | false)
and (b1 & b2 | b2 & b3 | b2 | b1) becomes (false | false | b2 | b1)


Thanks Anthony. I understand why it's false, but what I want to know is if the b2 in the first IF statement is evaluated after the second (and last) false (b3 & b2)? I was thinking that in that IF () since the last thing to be compared ends up being false, that the expression does not go back and search for more expressions to analyze so it just ends and in this case the whole expression is false.

I guess I could try out my theory by making b2 = true, but that would be soooo much work.


I think you answered my other question regardiong the union of the middle b2 or(|) with the left most and right most variables. The first b1&b2 = false first, then it becomes false|b2, not b2|b2.
Anthony O'Dea
Greenhorn

Joined: Jan 18, 2007
Posts: 8

Thanks Anthony. I understand why it's false, but what I want to know is if the b2 in the first IF statement is evaluated after the second (and last) false (b3 & b2)? I was thinking that in that IF () since the last thing to be compared ends up being false, that the expression does not go back and search for more expressions to analyze so it just ends and in this case the whole expression is false.


Once the ANDs are evaluated, the ORs are done (left-to-right, I assume). Not sure what you mean by evaluating b2, since its just a boolean value.

Also, since none of the operators are short-circuit operators (&& or ||), the entire expression would be evaluated.
 
jQuery in Action, 2nd edition
 
subject: Question from the SCJP 1.4 masterexam