Question:
What is the expected output of the following Java code?
public class operators {
public static void main(String[] args)
{
int a = 5;
int b = 7;
System.out.println("a&b = " + (a & b));
}
}
निम्न जावा कोड का संभावित आउटपुट क्या होगा?
public class operators {
public static void main(String[] args)
{
int a = 5;
int b = 7;
System.out.println("a&b = " + (a & b));
}
}
Options:
- a&b = 5
- a&b = 7
- a&b = 35
- a&b = 2
- a&b = 5
- a&b = 7
- a&b = 35
- a&b = 2
Correct Answer: a)
Explanation:
In Java, the `&` operator performs a bitwise AND operation on two integer operands:
– Value of \(a = 5\) in binary (4-bit): \(0101_2\)
– Value of \(b = 7\) in binary (4-bit): \(0111_2\)
– Bitwise AND operation:
\[\begin{matrix} & 0101_2 \\ \& & 0111_2 \\ \hline & 0101_2 = 5_{10} \end{matrix}\]
Hence, \((a \ \& \ b)\) results in \(5\), and the printed string is `”a&b = 5″`.
जावा में `&` ऑपरेटर बिटवाइज़ AND (Bitwise AND) संक्रिया करता है:
– \(a = 5\) का बाइनरी रूप: \(0101_2\)
– \(b = 7\) का बाइनरी रूप: \(0111_2\)
– बिटवाइज़ AND गणना:
\[\begin{matrix} & 0101_2 \\ \& & 0111_2 \\ \hline & 0101_2 = 5_{10} \end{matrix}\]
अतः `(a & b)` का मान \(5\) आता है, और कंसोल पर `a&b = 5` प्रिंट होगा।