Discussion
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));
}
}
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.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development