Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 10)
10.
What will be the output of the program?
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
Answer: Option
Explanation:
The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".
Discussion:
21 comments Page 2 of 3.
Neel said:
9 years ago
Nice explanation @Mayur.
Bhagyashree said:
1 decade ago
Please any one can explain it I have confusion.
Akash said:
1 decade ago
Thanks for the clear and detailed explanation.
Aravinth said:
1 decade ago
@Prikshit.
&& is used to define multiple conditions like if (i>100 && i<150).
But & is used for and operation. Same in case for || and | also.
&& is used to define multiple conditions like if (i>100 && i<150).
But & is used for and operation. Same in case for || and | also.
Quazi Faisal said:
1 decade ago
Thanks for such a good explanation.
Quazi Faisal said:
1 decade ago
Thanks for such a good explanation.
Prikshit said:
1 decade ago
Why is there no compilation error?
Don't we need to use && and || instead of & and |.
Don't we need to use && and || instead of & and |.
Mayur Raiyani said:
1 decade ago
Here is explanation:
First condition:
(b1 & b2 | b2 & b3 | b2).
(true & false | false & true | false).
(false | false | false).
(false).
Second Condition:
(b1 & b2 | b2 & b3 | b2 | b1).
(true & false | false & true | false | true).
(false | false | false | true).
(false | true).
(true).
First condition:
(b1 & b2 | b2 & b3 | b2).
(true & false | false & true | false).
(false | false | false).
(false).
Second Condition:
(b1 & b2 | b2 & b3 | b2 | b1).
(true & false | false & true | false | true).
(false | false | false | true).
(false | true).
(true).
Govind said:
1 decade ago
I can't understand please explain in the deeply.
Vinay said:
1 decade ago
First if is false
and second one is true.
Hint:
Truth table concept is working here apply that logic you will get ans why first if is false and second one is true.
and second one is true.
Hint:
Truth table concept is working here apply that logic you will get ans why first if is false and second one is true.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers