ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
struct M { operator int() const { return 7; } };
int main() { M m; cout << m + 1; }- A. 8
- B. 71
- C. Compile-time error: no operator+ for M
- D. 7
Correct answer: A. 8
A (non-explicit) conversion operator lets M be converted to int implicitly, so m + 1 becomes 7 + 1.
More OOP with C++ and Java questions
- What is printed? cpp // C++17 struct Counter { int n = 0; int operator()() { return ++n; } }; int main() { Counter c; c(); c(); Counter d…
- Which line fails to compile? cpp class B { public: void f() { cout << "f"; } void g() { cout << "g"; } }; class D : private B { public…
- What is printed? cpp struct A { void hi() { cout << "A"; } }; struct B { void hi(int) { cout << "B"; } }; struct C : A, B { }; int main()…
- What is printed? cpp struct A { virtual void f() { cout << "A"; } }; struct B { virtual void f() { cout << "B"; } }; struct C : A, B {…
- How many of these statements about C++ are correct? (i) A constructor cannot be declared virtual. (ii) A static member function cannot be…
- What is printed? cpp template <class T> void f(T) { cout << "1"; } template <> void f(int) { cout << "2"; } // explicit specialisation…