ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
class Base {
public:
    virtual void f() final { cout << "Base::f "; }
};
class Derived : public Base {
public:
    // void f() override { cout << "Derived::f "; } // line X
};
What happens if line X is uncommented?
  1. A. It compiles fine, overriding f()
  2. B. Compilation error: cannot override a function marked final
  3. C. It compiles, but f() becomes non-virtual in Derived
  4. D. It compiles only if Derived is also marked final

Correct answer: B. Compilation error: cannot override a function marked final

C++11's `final` specifier on a virtual function prohibits ANY further overriding in any derived class; Derived's attempt to override Base::f (which is final) is rejected by the compiler, regardless of using `override` correctly otherwise.

More OOP with C++ and Java questions

Exams that ask this topic