Virtual Functions and V-Table Interview Questions

    Question 1C++ OOPS Virtual Function Concept

    What is a virtual function in C++?

    Question 2C++ OOPS Virtual Function Syntax

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

    Question 3C++ 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 4C++ OOPS Non-Virtual Function Call

    What happens if virtual is removed in the above code?

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

    Which statement is true about pure virtual functions?

    Question 6C++ OOPS V-Table Concept

    What is the V-Table in C++?

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

    How is V-Table affected in multiple inheritance?

    Question 8C++ OOPS Virtual Destructor Importance

    Why should destructors be declared virtual in base classes?

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

    How does adding virtual functions affect object size?

    Question 10C++ 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(); }