Polymorphism Interview Questions

    Question 1: C++ OOPS - Polymorphism Concept

    What does polymorphism mean in C++?

    Question 2: C++ OOPS - Compile-time Polymorphism

    Which of the following supports compile-time polymorphism?

    Question 3: C++ OOPS - Function Overloading

    What will the following code output?

    #include<iostream> using namespace std; void show(int x) { cout << "Int"; } void show(double y) { cout << "Double"; } int main() { show(5.5); }

    Question 4: C++ OOPS - Operator Overloading

    Which operator cannot be overloaded in C++?

    Question 5: C++ OOPS - Virtual Function

    What will this code print?

    #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 6: C++ OOPS - Non-virtual Function

    What happens if `virtual` is removed in the above code?

    Question 7: C++ OOPS - Pure Virtual Function

    How do we declare a pure virtual function?

    Question 8: C++ OOPS - Abstract Class Example

    What will be the output?

    #include<iostream> using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() { cout << "Circle"; } }; int main() { Circle c; c.draw(); }

    Question 9: C++ OOPS - Function Overriding

    What will this print?

    #include<iostream> using namespace std; class A { public: virtual void show(){ cout<<"A"; } }; class B: public A { public: void show(){ cout<<"B"; } }; int main(){ B obj; obj.show(); }

    Question 10: C++ OOPS - Runtime Polymorphism Example

    What is the output?

    #include<iostream> using namespace std; class Animal { public: virtual void sound() { cout << "Animal"; } }; class Dog : public Animal { public: void sound() { cout << "Dog"; } }; int main() { Animal* a = new Dog(); a->sound(); }