ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS
Which statement about this code is correct?
Which statement about this code is correct?
struct B { virtual void f() const { } };
struct D : B { void f() override { } };- A. It compiles, and D::f overrides B::f
- B. It compiles, and D::f hides B::f without overriding it
- C. It fails to compile, because D::f marked override does not override anything
- D. It fails to compile, because a const function cannot be virtual
Correct answer: C. It fails to compile, because D::f marked override does not override anything
D::f is non-const while B::f is const, so the signatures differ and D::f does not override B::f. Without override it would silently hide B::f; with override the compiler must reject it.
More OOP with C++ and Java questions
- What is printed? cpp struct B { int x = 5; }; struct D : B { int x = 7; }; int main() { D d; B& b = d; cout << d.x << b.x << d.B::x; }
- What is printed? java class P { String f = "P"; static String s() { return "P"; } String i() { return "P"; } } class C extends P { String…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("L"); } static void f(Integer x) {…
- What is printed? java String x = "a"; final String y = "a"; System.out.print(("ab" == x + "b") + " "); System.out.print(("ab" == y + "b")…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static…
- What is printed? java class A { static int x = f(); static int y = 10; static int f() { return y 2; } } // in main: System.out.print(A.x +…