Vidyalelo
Java Programming · Q9

Operators

Programming · Java Programming · question 9

Q9

What is the result of the expression 5 | 3 in Java?

A.
7
Answer
B.
8
C.
1
D.
3

Answer: Option A

Solution

Answer: Option A
Solution:
Question: What is the result of the expression 5 | 3 in Java?

This question is about the bitwise OR operator (|) in Java. The bitwise OR operator compares the corresponding bits of two numbers. If either bit is 1, the resulting bit is 1; otherwise, it's 0.

Let's break it down:

First, we need to convert the decimal numbers 5 and 3 into their binary representations:

5 in decimal is 101 in binary
3 in decimal is 011 in binary

Now, let's perform the bitwise OR operation:

101 (5)
| 011 (3)
-------
111

The result in binary is 111. Converting 111 from binary back to decimal gives us 7.

Therefore, the expression 5 | 3 evaluates to 7.

Correct Answer: A