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, <, and equal to, ==, operator?
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 i:e true and false.

3. Which of the following operators can operate on a boolean variable?
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.

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 statement 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 no relate to zero or non zero numbers. true and false are boolean values.

6. What is the output of this program?

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

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:
$ javac Relational_operator.java
$ java Relational_operator
false

7. What is the output of this program?

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

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 is the output of this program?

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

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 is the output of this program?

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

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 is the output of this program?

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

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.

Subscribe Newsletter & Posts

Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Follow Manish & Sanfoundry