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 1 of 3.
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)
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)
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.
MUNNA PRAJAPATI said:
3 years ago
&& operator checks the next condition if the first will be true.
& operator checks all conditions whether the first is true or false.
So at Line 8 final output of if the condition expression is false
And at Line 10 final output of if the condition expression is true.
Therefore output would be "dokey".
& operator checks all conditions whether the first is true or false.
So at Line 8 final output of if the condition expression is false
And at Line 10 final output of if the condition expression is true.
Therefore output would be "dokey".
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).
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.
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.
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.
Rohit Singh Rathour said:
4 years ago
It should give compilation error because in java && and || operator is used for condition not single & |.
(1)
Preethi said:
7 years ago
I can't understand the short-circuit logical operator and boolean logical operator difference, can anyone explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers