What is the value of p in the following C++ code snippet?
int p;
bool a=true;
bool b=false;
int x=10;
int y=5;
p=((x|y)+(a+b));
cout<<p;
Options
-
A.
12
-
B.
16
-
C.
2
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
16
Detailed Explanation
Answer: (A) 12
First calculate the bitwise OR operation:
x = 10 and y = 5.
In binary:
10 = 1010
5 = 0101
Therefore:
10 | 5 = 1111 = 15
Now, a = true has the integer value 1 and b = false has the integer value 0.
Therefore:
a + b = 1 + 0 = 1
Finally:
p = 15 + 1 = 16
Therefore, the correct answer is actually (B) 16. In C++, true converts to 1 and false converts to 0 during arithmetic.