What is the output of the following C++ expression?
int x=6;
int y=3;
cout << x & y + 1;
Options
- A. 2
- B. 4
- D. More than one of the above
- E. None of the above
Quiz Practice:
int x=6;
int y=3;
cout << x & y + 1;
E. None of the above
Answer: (E) None of the above
This question tests operator precedence. The expression is:
x & y + 1
The addition operator + has higher precedence than the bitwise AND operator &. Therefore, it is evaluated as:
x & (y + 1)
Substituting the values:
6 & (3 + 1)
= 6 & 4
6 = 0110
4 = 0100
Therefore:
0110 & 0100 = 0100 = 4
Since 4 is not listed among A, B or C, the correct answer is (E) None of the above.