ProgrammingHardProduct-company MCQ pattern

What happens when this is compiled?

What happens when this is compiled?
struct A {
    virtual void f() final { cout << "A"; }
};
struct B : A {
    void f() override { cout << "B"; }
};
  1. A. Compiles fine; B::f overrides A::f normally
  2. B. Compile-time error: f() in A is final and cannot be overridden in B
  3. C. Compiles, but B::f is silently ignored at runtime
  4. D. Compile-time error: final cannot be combined with virtual

Correct answer: B. Compile-time error: f() in A is final and cannot be overridden in B

The final specifier on a virtual function in C++11 onward forbids any derived class from overriding it. B's attempt to override f() is rejected at compile time.

More OOP with C++ and Java questions

Exams that ask this topic