Study and learn Java MCQ questions and answers on Logical Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions. You can download these questions in PDF format. Just choose Print and select PDF as the format in Chrome or any other browser.
Go through Java Theory Notes on Logical Operators before reading questions.
Whether it is normal AND operator or Short Circuit AND operator, both operands should be TRUE to give output as true. If the first operand itself is false, there is no point in evaluating the second expression.
Both OR (|) and Short Circuit OR (||) operators give an output of true if at least one operand is true. Already the first operand is true. There is no need to evaluate or execute the second expression/operand.
Exclusive OR (^) gives an output of true if both the operands are different. If both are the same (true / false), the output is false. So, with just one operand, you can not decide the output.
Exclusive OR (^) gives an output of true if both the operands are different. If both are the same (true / false), the output is false. So, with just one operand, you can not decide the output.
! > & > ^ > | > && > || > Assignment
There no logical compound assignment operators like &&=, ||=, !=.
byte a= 1; if(!a) { System.out.println("FISH"); } else { System.out.println("CRAB"); }
You can not convert from byte to boolean.
The operator ! is undefined for the argument type(s) byte
int a=25, b=30; boolean c = a>25 & b<40; if(c) { System.out.println("RABBIT"); } else { System.out.println("GOOSE"); }
false & true is false only.
int a=25, b=30; boolean c = a>25 | b<40; if(c) { System.out.println("RABBIT"); } else { System.out.println("GOOSE"); }
false | true is true only.
int a=3, b=8; boolean c = a>5 && ++b>6; System.out.println(b);
++b>6 is not evaluated as the first operand itself is false. Short Circuit AND operator skips the second expression.
int a=5, b=9; boolean c = a>1 || b++<10; System.out.println(b);
b++<10 is not evaluated by the Short Circuit OR operator as the first operand is already true. There is no need to check the second expression.
int a=4, b=6, c=8; boolean d = a>5 && b>5 & c++<10; System.out.println(c);
Grouping or association of operands is done first based on the priority.
a>5 && (b>5 & c++<10) false && (anything) false
int a=4, b=8; boolean c = a>1 ^ b<10; if(c) { System.out.println("TREE"); } else { System.out.println("BIRD"); }
The exclusive operator (^) gives an output of TRUE only if both the operands are different.
int a=5; boolean b = a>1 || false; b ^= false; System.out.println(b);
b ^= false; b = b^false; b = true ^ false; b = true;
int a=4, b=8; boolean c = a>2 ^ b<10 & false; System.out.println(c);
AND has a higher priority than Exclusive OR.
a>2 ^ b<10 & false a>2 ^ (b<10 & false) a>2 ^ (true & false) a>2 ^ (false) true ^ false true
int a=3, b=1; int c = a & b; System.out.println("CAT " + c);
Here AND & operator is used with numbers. So, it is a Bitwise operator, not a Logical operator.