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));
}- A. 11
- B. 00
- C. 01
- 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
- What happens? cpp class Shape { public: virtual double area() const = 0; }; class Square : public Shape { double s = 2; double area()…
- What is printed? cpp struct Counter { int n = 0; Counter& add(int k) { n += k; return this; } Counter addCopy(int k) { n += k; return…
- Which statement about virtual functions and templates in C++ is correct?
- What is printed? cpp struct B { B() { cout << "B"; } B(const B&) { cout << "c"; } }; struct D : B { D() { cout << "D"; } D(const D&) {…
- What is printed? cpp struct P { int x; explicit P(int v) : x(v) { } explicit operator bool() const { return x != 0; } }; int main() { P…
- Which of the marked lines are compile errors? cpp int g(int x) { return x; } double g(int x) { return 2.0 x; } // line 1 void h(int x) { }…