ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
struct P {
    int x;
    explicit P(int v) : x(v) { }
    explicit operator bool() const { return x != 0; }
};
int main() {
    P a(0), b(3);
    if (b) cout << "T";
    bool c = static_cast<bool>(a);
    cout << c << !a;
}
  1. A. T01
  2. B. T00
  3. C. T11
  4. D. T10

Correct answer: A. T01

An explicit conversion to bool is still used in contextual conversions such as if (b) and the operand of !, so if (b) prints T and !a is 1. static_cast<bool>(a) gives 0. Only something like bool c = a; would fail to compile.

More OOP with C++ and Java questions

Exams that ask this topic