IntelliJ Java IDE
The moose likes Mock Exam Errata and the fly likes Short-Circuit Operators..... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "Short-Circuit Operators....." Watch "Short-Circuit Operators....." New topic
Author

Short-Circuit Operators.....

Mukti Bajaj
Ranch Hand

Joined: Dec 11, 2000
Posts: 59
Hello,
34. Given the following declaration which of the following statements equals to true
boolean b1 = true;
boolean b2 = false;
A) b1 == b2;
B) b1 | | b2;
C) b1 |& b2;
D) b1 && b2;
The answer to above questions is B...I think it should be B & D.
Can someone explain.....
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Mukti,
The surest way to check the results of Mock questions is to write some code and see what happens

The short-circuit operators give the same results as the normal | and & operators.
Hope that helps.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Hi Mukti,
I fully agree with you. D should be "true" since "b1" evaluates to true and because of the short circuit operator, the whole expression becomes true!
Mukti Bajaj
Ranch Hand

Joined: Dec 11, 2000
Posts: 59
Hi Vineet,
The answer to the above question is only B and not D.
As for boolean OR operators, | and | | if either or both operands are true,the result is true and you will get result false, only if both the operands are false.
And for boolean AND operators, & and && if both the operands are true the result is true otherwise the result is false.
So, I agree with Jane suggestions....of writing the code and checking.
Good Luck!
Mukti
 
IntelliJ Java IDE
 
subject: Short-Circuit Operators.....
 
Threads others viewed
Boolean
help required
ternary operator doubt
R these equal?
logical operator doubt
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com