Polymorphism Interview Questions

    Question 1C++ OOPS - Polymorphism Concept

    What does polymorphism mean in C++?

    Question 2C++ OOPS - Compile-time Polymorphism

    Which of the following supports compile-time polymorphism?

    Question 3C++ 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 4C++ OOPS - Operator Overloading

    Which operator cannot be overloaded in C++?

    Question 5C++ 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 6C++ OOPS - Non-virtual Function

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

    Question 7C++ OOPS - Pure Virtual Function

    How do we declare a pure virtual function?

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