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?- A. It compiles fine, overriding f()
- B. Compilation error: cannot override a function marked final
- C. It compiles, but f() becomes non-virtual in Derived
- 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
- What is printed? cpp struct Base { Base() { cout << "Base() "; } }; struct Mixin1 { Mixin1() { cout << "Mixin1() "; } }; struct Mixin2 {…
- What is printed? java enum Operation { ADD { public int apply(int a, int b) { return a + b; } }, MULTIPLY { public int apply(int a, int b)…
- What is printed? cpp class Shape { public: Shape& operator=(const Shape&) = delete; Shape() = default; }; int main() { Shape a, b; a = b; }
- What is printed? java class Parent { protected int score = 50; int getScore() { return score; } } class Child extends Parent { Child() {…
- What is printed? java class Base { void greet() { System.out.print("Base "); helper(); } private void helper() {…
- What is printed? cpp struct Base { virtual void save() { cout << "BaseSave "; } }; struct Derived : Base { void save() override { cout <<…