• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Short-Circuit Operators.....

 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic