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.
Ratan Pal said:
9 years ago
Great explanation @Mayur.
Neel said:
9 years ago
Nice explanation @Mayur.
Suresh said:
9 years ago
Nice explanation. Thank for explaining it.
Bansal said:
8 years ago
The difference between (&& or &) and (|| or |) is only that (&& and ||) performs a short-circuit whereas (& and |) does not otherwise operations of all && and &,(|| or | ) is same both as AND, OR operators.
Chander Kant said:
8 years ago
"if" only evaluates true condition. i.e. if(true) entry and if(false) get out.
Preethi said:
7 years ago
I can't understand the short-circuit logical operator and boolean logical operator difference, can anyone explain?
Sangeetha R said:
7 years ago
Logical AND (&&):
It evaluates to "true" if and only "if both operands of logical AND are true". If either or both operands are false, it evaluates to false.
Logical OR (||):
It evaluates to true if "either or both" of its operands are "true". If both operands are false, it evaluates to false.
It evaluates to "true" if and only "if both operands of logical AND are true". If either or both operands are false, it evaluates to false.
Logical OR (||):
It evaluates to true if "either or both" of its operands are "true". If both operands are false, it evaluates to false.
Siva said:
6 years ago
"if" only evaluates true condition. i.e. if(true) entry and if(false) get out.
if ( b1 & b2 | b2 & b3 | b2 ){ /* Line 8 */ flase | flase | flase| ---> if(false) get out.
System.out.print("ok ");
}
if ( b1 & b2 | b2 & b3 | b2 | b1 ){ /*Line 10*/ flase | flase | flase| true---->"if" only evaluates true condition
System.out.println("dokey");
o/p:-- dokey
if ( b1 & b2 | b2 & b3 | b2 ){ /* Line 8 */ flase | flase | flase| ---> if(false) get out.
System.out.print("ok ");
}
if ( b1 & b2 | b2 & b3 | b2 | b1 ){ /*Line 10*/ flase | flase | flase| true---->"if" only evaluates true condition
System.out.println("dokey");
o/p:-- dokey
(1)
Rohit Singh Rathour said:
4 years ago
It should give compilation error because in java && and || operator is used for condition not single & |.
(1)
Manjit said:
3 years ago
@All.
First condition:
(b1 & b2 | b2 & b3 | b2).
(true & false | false & true | false)
(1&0|0&1|0)
(false | false | false).
(0|0|0)
(false).
(0)
Second Condition:
(b1 & b2 | b2 & b3 | b2 | b1).
(true & false | false & true | false | true).
(1&0|0&1|0|1)
(false | false | false | true).
(0|0|0|1)
(false | true).
(0|1)
(true).
(1).
First condition:
(b1 & b2 | b2 & b3 | b2).
(true & false | false & true | false)
(1&0|0&1|0)
(false | false | false).
(0|0|0)
(false).
(0)
Second Condition:
(b1 & b2 | b2 & b3 | b2 | b1).
(true & false | false & true | false | true).
(1&0|0&1|0|1)
(false | false | false | true).
(0|0|0|1)
(false | true).
(0|1)
(true).
(1).
(3)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers