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));
}
}
DSSSB TGT - 2023,
Answer: A
The output is a&b = 5. The string "a&b = " is printed as it is, and the expression (a & b) uses the bitwise AND operator on 5 and 7. \[ 5 = 0101_2,\quad 7 = 0111_2 \] \[ 0101_2 \;\&\; 0111_2 = 0101_2 = 5 \] A bit is 1 only where both operands have a 1. The value 7 would be the result of OR, 35 of multiplication, and 2 does not come from either. In Java, the operator & on integers is bitwise AND, and on booleans it is a logical AND, so 5 & 7 gives the number 5 here.
Please log in or register to report this question.