+1 vote
in C Plus Plus by

What is the value of p in the following C++ code snippet?

  1. #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int p;
  6.         bool a = true;
  7.         bool b = false;
  8.         int x = 10;
  9.         int y = 5;
  10.         p = ((x | y) + (a + b));
  11.         cout << p;
  12.         return 0;
  13.     }

a) 12
b) 0
c) 2
d) 16

1 Answer

0 votes
by

Answer: d
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final value of expression in line #10 will be 15 + 1 = 16.

Related questions

0 votes
asked Oct 19, 2022 in C Plus Plus by AdilsonLima
0 votes
asked Jan 27, 2020 in TypeScript - JavaScript's Superset by AdilsonLima
...