What will be the output of the following C++ code?
bool a=true;
bool b=true;
int result=a+b;
cout << result;
Options
- B. 1
- C. 2
- D. More than one of the above
- E. None of the above
Quiz Practice:
bool a=true;
bool b=true;
int result=a+b;
cout << result;
C. 2
Answer: (C) 2
In C++, a Boolean value participates in arithmetic by converting true to 1 and false to 0.
Here:
a = true = 1
b = true = 1
Therefore:
result = a + b = 1 + 1 = 2
Hence, the output is 2.