ProgrammingModerateMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
struct X {
    int v;
    X(int a) : v(a) { }
    bool operator==(const X& o) const { return v == o.v; }
};
int main() {
    X a(1);
    cout << (a == 1) << (a == X(2));
}
  1. A. 11
  2. B. 00
  3. C. 01
  4. D. 10

Correct answer: D. 10

1 converts to X through the non-explicit constructor, so a == 1 compares 1 with 1 (true, printed as 1). a == X(2) compares 1 with 2 (0).

More OOP with C++ and Java questions

Exams that ask this topic