Boolean Operators in java and Priority Tutorial and MCQ
Table of Contents
Study and learn Boolean Operators in java questions and answers on Java Boolean Logical Operators and their priorities. Attend job interviews easily with these Multiple Choice Questions.
Boolean Operators
Java Boolean Logical Operators work with boolean data type values. Some logical operators work with a single Operand while others work with two Operands. There are bitwise logical operators too which we discuss later.
Boolean Operators with Priority
The list of Boolean Logical operators is given below. Note that the Logical Unary NOT (!) has got the highest priority among all other logical operators. The assignment operator has the least priority.
Note 1: Except for Logical NOT(!), all other logical operators have less priority than Arithmetic and Relational operators.
Priority | Operator | Simple Name |
---|---|---|
1 | ! | Logical Unary NOT |
2 | & | Logical AND |
3 | ^ | Exclusive OR or XOR |
4 | | | Logical OR |
5 | && | Logical Short Circuit AND |
6 | || | Logical Short Circuit OR |
7 | op= | Compound Assignment Operator&=, |=, ^= |
7 | = | Assignment |
Java Boolean Operators
Note 2: Generally, software programmers use only Short Circuit Logical operators compared to normal logical operators for good performance and to differentiate bitwise AND and OR from Logical AND and OR operators. If you use AND (&) operator, it generally means you are using it in the context of bitwise operations.
Example with Precedence or Priority Explained
In the below code example, Relational operator (>) takes higher priority. Short Circuit AND (&&) has got the least priority.
class OperatorPrecedence { public static void main(String args[]) { int a=5; int b=9; boolean c = a > 4 && true & b !=9; // Priority order: >, !=, &, && // (a>4) && true & b!=9 // true && true & b==9 // true && true & false // true && false //false System.out.println(c); } } //OUTPUT: false
1. Logical Unary NOT (!) Operator
Logical Unary NOT operator requires just One Operand. It simply turns true to false and false to true. Its symbol is an Exclamation.
Expression | Result |
---|---|
false | true |
true | false |
Boolean Operators Examples
Example Usage:
boolean show = true; show = !show; if(!show) { System.out.println("SHOW = false"); }
2. Logical AND (&) Operator
Logical AND operator gives an output of true only if both operands are true. Logical AND operator checks the value of both operands or expressions before giving output. Even if the first expression is false, it goes for evaluating the second expression. This is a time waste thing and a drawback. Its symbol is an Ampersand.
expression1 | expression2 | Result |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
Example Usage:
int k=5, p=9; c = (k<4)&(p>5); //p>5 is evaluated. Time waste. if(c) { System.out.println("Logical AND"); }
3. Logical Short Circuit AND (&&) Operator
Logical Short Circuit AND gives an output of true only if both Operands or Expressions are true. If the first expression is false, Short Circuit AND (&&) avoids evaluating the second expression. So it is faster than normal Logical AND (&) operator. Its symbol is Two Ampersands (&&).
expression1 | expression2 | Result |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
Example Usage:
int k=5, p=9; c = (k<4)&&(p>5); //p>5 is not evaluated. if(!c) { System.out.println("Short Circuit Logical AND"); }
4. Logical OR (|) Operator
The logical OR (|) operator gives an output of true if one of the operands is true. So a combination of true and false always gives true as the output. Logical OR evaluates the second expression or operand even if the first expression is true. So it is slow compared to Short Circuit Logical Operator (||). Its symbol is a PIPE.
expression1 | expression2 | Result |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Example Usage:
int k=5, p=9; c = (k>3)|(p>5); //p>5 is evaluated. Time waste. if(c) { System.out.println("Logical OR"); }
5. Logical Short Circuit OR (||) Operator
The Logical Short Circuit OR (||) operator gives an output of true if one of the expressions or operands is true. It gives an output of true if the first expression is true without evaluating the second expression. So it is fast. Only if the first expression is false, the second expression is evaluated or executed by Short Circuit OR operator.
expression1 | expression2 | Result |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Its symbol is Two Pipes (||).
Example Usage:
int k=5, p=9; c = (k>3)||(p>5); //p>5 is not evaluated. if(c) { System.out.println("Short Circuit Logical OR"); }
6. Logical Exclusive OR (^) Operator
The Logical Exclusive OR (^) operator gives an output of true if both operands are different. It gives an output of false if both the operands or expressions are the same. Its symbol is a CARAT.
expression1 | expression2 | Result |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | false |
Example Usage:
int k=5, p=9; c = (k>3)^(p>9); if(c) { System.out.println("Logical XOR"); }
Java Compound Logical Assignment Operators
There are only three Compound Logical Assignment operators in Java, AND, OR and Exclusive OR. Simply put an Equal To (=) symbol after the Logical AND, OR or XOR operators to turn it into a Compound Assignment operator.
Note: The compound assignment is not possible with Short Circuit AND (&&), Short Circuit OR (||) and Unary NOT(!) operators.
1. Compound Assignment AND (&=) Operator
Here “expression” should be boolean variable or constant. “var” is a variable.
Syntax:
var &= expression var = var & expression eg. a &= false a = a & false
2. Compound Assignment OR(|=) Operator
Here “expression” should be boolean variable or constant. “var” is a variable.
Syntax:
var |= expression var = var | expression eg. a |= true a = a | true
3. Compound Assignment Exclusive OR (^=) Operator
Here “expression” should be boolean variable or constant. “var” is a variable.
Syntax:
var ^= expression var = var ^ expression eg. a ^= true a = a ^ true
Example: Logical Compound Assignment Operators
class CompoundAssignmentOperators { public static void main(String args[]) { boolean a = true; boolean b= false; boolean c = true; boolean d = false; a &= false; //a = a & false = false b |= true; //b = b | true = true c ^= false; //c = c^false = true d &&= false; //Error. Not an Operator d ||= false; //Error } }
[WpProQuiz 68]
Boolean Operators MCQ
1) What are the two possible Logical Operator types?
A) Bitwise Logical
B) Boolean Logical
C) Arithmetic Logical
D) A and B
Answer [=] D
2) Boolean logical operators in Java work with?
A) true/false boolean data
B) 1 and 0 of individual Bits
C) characters of a String
D) None of the above
Answer [=] A
3) Bitwise logical operators in Java work with?
A) true/false boolean data
B) 0 and 1 individual bits of data
C) Characters of a String
D) None of the above
Answer [=] B
4) In general Bitwise logical operators are simply called?
A) Logical operators
B) Bitwise operators
C) Binary operators
D) None of the above
Answer [=] B
Explanation:
Yes. Do not call Bitwise operators by adding the word Logical. It is confusing for any one.
5) What is the input for Logical Operators?
A) 1 and 0
B) true / false
C) char / String
D) None of the above
Answer [=] B
6) What is the output of any Logical operation in Java?
A) 1 or 0
B) true or false
C) char or String
D) None of the above
Answer [=] B
7) Which is the Logical operator in Java that works with a Single Operand?
A) Logical AND
B) Logical OR
C) Logical Exclusive OR
D) Logical NOT
Answer [=] D
8) Which among the following is a Logical Unary NOT operator in Java?
A) ~
B) !
C) #
D) ^
Answer [=] B
Explanation:
~ is a Bitwise Unary NOT that complements bits from 0 to 1 and 1 to 0.
9) What is the output of the Java code snippet?
boolean b=false; b = !b; System.out.println(b);
A) true
B) false
C) Compiler error
D) None of the above
Answer [=] A
10) Which among the following is a Short Circuit AND operator?
A) &
B) &&
C) |
D) ||
Answer [=] B
Explanation:
Two ampersands &&
11) Which among the following is a Short Circuit OR operator?
A) &
B) &&
C) |
D) ||
Answer [=] D
Explanation:
Two Pipes ||
12) What is the output of a Logical OR (|) operation if one of the inputs/operands is false?
A) false
B) true
C) true or false
D) None of the above
Answer [=] C
Explanation:
Logical OR operator give true as output if at least one of the operands is true.
13) What is the output of Logical AND (&) operation if one of the inputs/operands is false?
A) false
B) true
C) true or false
D) None of the above
Answer [=] A
Explanation:
false & (anything) is false.
14) What is the output of a Logical OR (|) operation if one of the inputs/operands is true?
A) false
B) true
C) true or false
D) None of the above
Answer [=] B
Explanation:
true | (anything) is true.
15) What is the output of a Logical AND (&) operation if one of the inputs/operands is true?
A) false
B) true
C) true of false
D) None of the above
Answer [=] C
Explanation:
true & (anything) = true or false
16) What is the output of a Logical AND (&) operation if both inputs/operands are true?
A) false
B) true
C) true or false
D) None of the above
Answer [=] B
17) What is the output of a Logical OR (|) operation if both the inputs/operands are true?
A) true
B) false
C) true or false
D) None of the above
Answer [=] A
18) Which is fast among AND (&) and Short Circuit AND(&&) operators in Java?
A) AND operator
B) Short Circuit AND
C) Both work at the same speed
D) None of the above
Answer [=] B
19) Which is fast among OR(|) and Short Circuit OR (||) operators in Java?
A) OR Operator
B) Short Circuit OR operator
C) Both work at the same speed
D) None of the above
Answer [=] B
20) Why are Short Circuit AND (&&) and Short Circuit OR (||) operators are fast in Java?
A) By skipping the second expression or operand if possible and save time.
B) By using extra memory on the machine
C) By using extra CPU processing power
D) None of the above
Answer [=] A
Explanation:
Short Circuit operators are not 100% every time. Based on the value of the First operand, they decide whether to go evaluating the second expression.