Java Questions & Answers – Bitwise Operators

This section of our 1000+ Java MCQs focuses on Bitwise operators of Java Programming Language.

1. Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=
View Answer

Answer:d
Explanation: <= is a relational operator.

2. Which operator is used to invert all the digits in binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^
View Answer

Answer:a
Explanation: Unary not operator, ~, inverts all of the bits of its operand in binary representation.

3. On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?
a) 1
b) 32
c) 33
d) 31
View Answer

Answer: d
Explanation: The left shift operator shifts all of the bite in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

4. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=
View Answer

Answer: b
Explanation: None.

5. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bite in a value to the left specified number of times.
b) The right shift operator, >>, shifts all of the bite in a value to the right specified number of times.
c) The left shift operator can be used as an alternative to multiplying by 2.
d) The right shift operator automatically fills the higher order bits with 0.
View Answer

Answer: d
Explanation: The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value.

6. What is the output of this program?

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

a) 42 42
b) 43 43
c) 42 -43
d) 42 43
View Answer

Answer:c
Explanation: Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43

7. What is the output of this program?

  1.     class bitwise_operator {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int a = 3;
  5.              int b = 6;
  6.  	     int c = a | b;
  7.              int d = a & b;             
  8.              System.out.println(c + " "  + d);
  9.         } 
  10.     }

a) 7 2
b) 7 7
c) 7 5
d) 5 2
View Answer

Answer: a
Explanation: And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
7 2

8. What is the output of this program?

  1.     class leftshift_operator {
  2.         public static void main(String args[]) 
  3.         {        
  4.              byte x = 64;
  5.              int i;
  6.              byte y; 
  7.              i = x << 2;
  8.              y = (byte) (x << 2)
  9.              System.out.print(i + " " + y);
  10.         } 
  11.     }

a) 0 64
b) 64 0
c) 0 256
d) 256 0
View Answer

Answer:d
Explanation: None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0

9. What is the output of this program?

  1.     class rightshift_operator {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int x; 
  5.              x = 10;
  6.              x = x >> 1;
  7.              System.out.println(x);
  8.         } 
  9.     }

a) 10
b) 5
c) 2
d) 20
View Answer

Answer: b
Explanation: Right shift operator, >>, devides the value by 2.
output:
$ javac rightshift_operator.java
$ java rightshift_operator
5

10. What is the output of this program?

  1.     class Output {
  2.         public static void main(String args[]) 
  3.         {    
  4.              int a = 1;
  5.              int b = 2;
  6.              int c = 3;
  7.              a |= 4;
  8.              b >>= 1;
  9.              c <<= 1;
  10.              a ^= c;
  11.              System.out.println(a + " " + b + " " + c);
  12.         } 
  13.     }

a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
View Answer

Answer: a
Explanation: None.
output:
$ javac Output.java
$ java Output
3 1 6

Sanfoundry Global Education & Learning Series – Java Programming Language.

Subscribe Newsletter & Posts
About

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.


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