Java MCQs on relational operators and boolean logic operators of Java Programming Language.
1. What is the output of relational operators? Answer: b 2. Which of these is returned by “greater than”, “less than” and “equal to” operators? Answer: c 3. Which of the following operators can operate on a boolean variable? a) 3 & 2 Answer: d 4. Which of these operators can skip evaluating right hand operand? Answer: d 5. Which of these statements is correct? Answer: d 6. What will be the output of the following Java code? a) 1 7. What will be the output of the following Java code? a) false false 8. What will be the output of the following Java code? a) 0 9. What will be the output of the following Java code? a) 1 10. What will be the output of the following Java code? a) 0
a) Integer
b) Boolean
c) Characters
d) Double
Clarification: None.
a) Integers
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Clarification: All relational operators return a boolean value ie. true and false.
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3
Clarification: Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.
a) !
b) |
c) &
d) &&
Clarification: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.
a) true and false are numeric values 1 and 0
b) true and false are numeric values 0 and 1
c) true is any non zero value and false is 0
d) true and false are non numeric values
Clarification: True and false are keywords, they are non numeric values which do not relate to zero or non zero numbers. true and false are boolean values.
class Relational_operator
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
System.out.print(var1 > var2);
}
}
b) 0
c) true
d) false
Clarification: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output:
$ javac Relational_operator.java
$ java Relational_operator
false
class bool_operator
{
public static void main(String args[])
{
boolean a = true;
boolean b = !true;
boolean c = a | b;
boolean d = a & b;
boolean e = d ? b : c;
System.out.println(d + " " + e);
}
}
b) true ture
c) true false
d) false true
Clarification: Operator | returns true if any one operand is true, thus ‘c = true | false’ is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d is false thus e = d ? b : c , assigns c to e , e contains true.
output:
$ javac bool_operator.java
$ java bool_operator
false true
class ternary_operator
{
public static void main(String args[])
{
int x = 3;
int y = ~ x;
int z;
z = x > y ? x : y;
System.out.print(z);
}
}
b) 1
c) 3
d) -4
Clarification: None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3
class Output
{
public static void main(String args[])
{
int x , y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program
Clarification: Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2
class Output
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a ^ b;
System.out.println(!c);
}
}
b) 1
c) false
d) true
Clarification: None.
output:
$ javac Output.java
$ java Output
false