Polymorphism means one interface can represent different types of objects. It allows a single function or method to work in different ways depending on the object it is acting on.
Click to reveal answer
beginner
Why do we need polymorphism in programming?
Polymorphism helps write flexible and reusable code. It lets us use the same code to work with different types of objects, making programs easier to extend and maintain.
Click to reveal answer
intermediate
How does polymorphism improve code maintenance?
With polymorphism, you can add new object types without changing existing code. This means less chance of bugs and easier updates.
Click to reveal answer
beginner
What is an example real-life situation for polymorphism?
Think of a remote control that works with many devices like TV, DVD player, or stereo. The remote uses the same buttons but each device reacts differently. This is like polymorphism in code.
Click to reveal answer
intermediate
What role do virtual functions play in polymorphism?
Virtual functions allow C++ to decide at runtime which function to call based on the object type. This is key to achieving polymorphism.
Click to reveal answer
What does polymorphism allow in C++?
AOne interface to work with different object types
BOnly one function per class
CVariables to change type automatically
DFunctions to run faster
✗ Incorrect
Polymorphism allows one interface to represent different types of objects.
Why is polymorphism useful for code maintenance?
AIt allows adding new types without changing existing code
BIt forces rewriting all code
CIt makes code run slower
DIt removes the need for classes
✗ Incorrect
Polymorphism lets you add new object types without changing existing code, making maintenance easier.
Which C++ feature is essential for runtime polymorphism?
ATemplates
BVirtual functions
CMacros
DInline functions
✗ Incorrect
Virtual functions enable C++ to decide at runtime which function to call, enabling polymorphism.
Polymorphism helps make code:
ALess flexible
BHarder to understand
CMore reusable
DOnly work with one object type
✗ Incorrect
Polymorphism makes code more reusable by allowing the same code to work with different object types.
A remote control working with TV, DVD, and stereo is an example of:
AInheritance
BEncapsulation
CAbstraction
DPolymorphism
✗ Incorrect
The remote control example shows polymorphism because the same interface controls different devices differently.
Explain why polymorphism is important in C++ programming.
Think about how one function can work with many object types.
You got /4 concepts.
Describe a real-life example that helps understand polymorphism.
Consider devices controlled by one remote.
You got /3 concepts.
Practice
(1/5)
1. Why is polymorphism important in C++ programming?
easy
A. It allows one interface to work with different object types.
B. It makes the program run faster by using less memory.
C. It prevents any changes to the code once compiled.
D. It forces all functions to have the same name.
Solution
Step 1: Understand polymorphism concept
Polymorphism means one interface can represent many forms or object types.
Step 2: Identify the benefit in code flexibility
This allows calling the correct function automatically depending on the object type, making code flexible and reusable.
Final Answer:
It allows one interface to work with different object types. -> Option A
Quick Check:
Polymorphism = One interface, many types [OK]
Hint: Polymorphism means many forms, one interface [OK]
Common Mistakes:
Thinking polymorphism improves speed directly
Confusing polymorphism with code immutability
Believing polymorphism forces identical function names
2. Which of the following is the correct way to declare a virtual function in C++?
easy
A. void virtual func();
B. void func() virtual;
C. virtual void func();
D. virtual func void();
Solution
Step 1: Recall virtual function syntax
In C++, the keyword 'virtual' comes before the return type in the function declaration.
Step 2: Match correct syntax
Only 'virtual void func();' matches the correct syntax for declaring a virtual function.
Final Answer:
virtual void func(); -> Option C
Quick Check:
Virtual keyword before return type [OK]
Hint: Virtual keyword always precedes return type [OK]
Common Mistakes:
Placing 'virtual' after function name
Mixing order of keywords
Omitting parentheses in declaration
3. What will be the output of the following C++ code?
class Base {
public:
virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived"; }
};
int main() {
Base* ptr = new Derived();
ptr->show();
delete ptr;
return 0;
}
medium
A. Runtime error
B. Base
C. Compilation error
D. Derived
Solution
Step 1: Understand virtual function behavior
The 'show' function is virtual in Base and overridden in Derived, so the call uses Derived's version.
Step 2: Analyze pointer type and object
Pointer is Base* but points to Derived object, so ptr->show() calls Derived::show(), printing "Derived".
Final Answer:
Derived -> Option D
Quick Check:
Virtual function calls Derived version [OK]
Hint: Virtual calls use actual object type, not pointer type [OK]
Common Mistakes:
Assuming Base version prints because pointer type is Base*
Confusing override keyword effect
Ignoring virtual function behavior
4. Identify the error in this C++ code related to polymorphism:
class Animal {
public:
void sound() { std::cout << "Animal sound"; }
};
class Dog : public Animal {
public:
void sound() { std::cout << "Bark"; }
};
int main() {
Animal* a = new Dog();
a->sound();
delete a;
return 0;
}
medium
A. Missing virtual keyword in base class function.
B. Dog class should not inherit from Animal.
C. Pointer type should be Dog*, not Animal*.
D. sound() function should return a string.
Solution
Step 1: Check function declaration in base class
The base class function 'sound' is not declared virtual, so polymorphism won't work.
Step 2: Understand effect on function call
Because 'sound' is not virtual, calling a->sound() calls Animal::sound(), not Dog::sound().
Final Answer:
Missing virtual keyword in base class function. -> Option A
Quick Check:
Base function must be virtual for polymorphism [OK]
Hint: Base class functions must be virtual for polymorphism [OK]
Common Mistakes:
Thinking inheritance alone enables polymorphism
Using wrong pointer type for polymorphism
Expecting return type change to fix polymorphism
5. You want to design a system where different shapes (Circle, Square) can be drawn using the same interface. Which approach best uses polymorphism in C++?
hard
A. Define draw() as a static method in each shape class.
B. Create a base class Shape with a virtual draw() method, and override draw() in Circle and Square classes.
C. Use global functions drawCircle() and drawSquare() without classes.
D. Create separate classes Circle and Square without any base class and call their draw() directly.
Solution
Step 1: Identify polymorphism design pattern
Polymorphism requires a common interface with virtual functions to allow different behaviors.
Step 2: Match design to polymorphism
Base class Shape with virtual draw() lets Circle and Square override draw(), enabling polymorphic calls.
Final Answer:
Create a base class Shape with a virtual draw() method, and override draw() in Circle and Square classes. -> Option B
Quick Check:
Virtual base method with overrides enables polymorphism [OK]
Hint: Use virtual base method and override in derived classes [OK]
Common Mistakes:
Not using a base class for common interface
Using static methods which don't support polymorphism
Using separate global functions instead of class methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.