ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct A { virtual void f() { cout << "A"; } };
struct B { virtual void f() { cout << "B"; } };
struct C : A, B { void f() override { cout << "C"; } };
int main() {
C c;
A& a = c; B& b = c;
a.f(); b.f();
}- A. AB
- B. CB
- C. Compile-time error: C::f overrides two functions
- D. CC
Correct answer: D. CC
One function in C with the matching signature overrides f in BOTH bases, so calls through either base reference dispatch to C::f.
More OOP with C++ and Java questions
- 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…
- What is printed? cpp template <int N> struct F { static const int v = N F<N - 1>::v; }; template <> struct F<0> { static const int v = 1…
- What is printed? cpp struct M { M() { throw 1; } }; struct S { M m; S() try : m() { } catch (int) { cout << "S"; } }; int main() { try { S…
- What is printed? cpp void f() noexcept { } void g() { } int main() { cout << noexcept(f()) << noexcept(g()); }
- What happens when this is compiled? cpp int main() { try { throw 1; } catch (...) { cout << "any"; } catch (int) { cout << "int"; } }