Java Questions & Answers – Relational Operators and Boolean Logic Operators

This section of our 1000+ Java MCQs focuses on relational operators and boolean logic operators of Java Programming Language.

1. What is the output of relational operators?
a) Integer
b) Boolean
c) Characters
d) Double
View Answer

Answer: b
Explanation: None.

2. Which of these is returned by “greater than”, “less than” and “equal to” operators?
a) Integers
b) Floating – point numbers
c) Boolean
d) None of the mentioned
View Answer

Answer: c
Explanation: All relational operators return a boolean value ie. true and false.

3. Which of the following operators can operate on a boolean variable?

advertisement
advertisement
   1. &&
   2. ==
   3. ?:
   4. +=

a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3
View Answer

Answer: d
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&
View Answer

Answer: d
Explanation: Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.

5. Which of these statements is correct?
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
View Answer

Answer: d
Explanation: 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.
advertisement

6. What will be the output of the following Java code?

  1.     class Relational_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 5; 
  6.             int var2 = 6;
  7.             System.out.print(var1 > var2);
  8.         } 
  9.     }

a) 1
b) 0
c) true
d) false
View Answer

Answer: d
Explanation: Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output:

advertisement
$ javac Relational_operator.java
$ java Relational_operator
false

7. What will be the output of the following Java code?

  1.     class bool_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = !true;
  7.              boolean c = a | b;
  8.  	     boolean d = a & b;
  9.              boolean e = d ? b : c;
  10.              System.out.println(d + " " + e);
  11.         } 
  12.     }

a) false false
b) true ture
c) true false
d) false true
View Answer

Answer: d
Explanation: 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

8. What will be the output of the following Java code?

  1.     class ternary_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              int x = 3;
  6.              int y = ~ x;
  7.              int z;
  8.              z = x > y ? x : y;
  9.              System.out.print(z);
  10.         } 
  11.     }

a) 0
b) 1
c) 3
d) -4
View Answer

Answer: c
Explanation: None.
output:

$ javac ternary_operator.java
$ java ternary_operator
3

9. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x , y = 1;
  6.              x = 10;
  7.              if (x != 10 && x / 0 == 0)
  8.                  System.out.println(y);
  9.              else
  10.                  System.out.println(++y);
  11.         } 
  12.     }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program
View Answer

Answer: b
Explanation: 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

10. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = false;
  7.              boolean c = a ^ b;
  8.              System.out.println(!c);
  9.         } 
  10.     }

a) 0
b) 1
c) false
d) true
View Answer

Answer: c
Explanation: None.
output:

$ javac Output.java
$ java Output
false
Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.