Virtual Functions and V-Table Interview Questions

    Question 1: C++ OOPS Virtual Function Concept

    What is a virtual function in C++?

    Question 2: C++ OOPS Virtual Function Syntax

    Which keyword is used to declare a virtual function in C++?

    Question 3: C++ OOPS Virtual Function Example

    What will be the output?

    #include <iostream> using namespace std; class Base { public: virtual void show() { cout << "Base"; } }; class Derived : public Base { public: void show() { cout << "Derived"; } }; int main() { Base* b = new Derived(); b->show(); }

    Question 4: C++ OOPS Non-Virtual Function Call

    What happens if virtual is removed in the above code?

    Question 5: C++ OOPS Pure Virtual Function & V-Table

    Which statement is true about pure virtual functions?

    Question 6: C++ OOPS V-Table Concept

    What is the V-Table in C++?

    Question 7: C++ OOPS Multiple Inheritance & V-Table

    How is V-Table affected in multiple inheritance?

    Question 8: C++ OOPS Virtual Destructor Importance

    Why should destructors be declared virtual in base classes?

    Question 9: C++ OOPS V-Table and Object Size

    How does adding virtual functions affect object size?

    Question 10: C++ OOPS Virtual Function Override Example

    What will be the output?

    #include <iostream> using namespace std; class A { public: virtual void f() { cout << "A"; } }; class B : public A { public: void f() { cout << "B"; } }; class C : public B { public: void f() { cout << "C"; } }; int main() { A* p = new C(); p->f(); }