Polymorphism Question Bank for C-CAT
Topic-wise Polymorphism MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: A - Function overloading
Function overloading is resolved at compile time (static polymorphism).
Show Answer & Explanation
Correct Answer: A - Virtual functions
Virtual functions enable runtime polymorphism through dynamic binding.
Show Answer & Explanation
Correct Answer: C - virtual void f() = 0
Pure virtual function is declared with = 0.
Show Answer & Explanation
Correct Answer: A - At least one pure virtual function
A class with at least one pure virtual function is abstract.
Show Answer & Explanation
Correct Answer: B - Only inherited
Abstract classes cannot be instantiated, only inherited.
Show Answer & Explanation
Correct Answer: B - Parameter list
Overloading is resolved by different parameter list (number/type).
Show Answer & Explanation
Correct Answer: D - ::
Scope resolution operator (::) cannot be overloaded.
Show Answer & Explanation
Correct Answer: D - Using base pointer to delete derived object
Virtual destructor ensures proper cleanup when deleting via base pointer.
Show Answer & Explanation
Correct Answer: C - Classes with virtual functions
Virtual pointer (vptr) is created for classes with virtual functions.
Show Answer & Explanation
Correct Answer: D - Runtime
Late binding (dynamic binding) occurs at runtime.
Show Answer & Explanation
Correct Answer: B - Giving operators new meaning for user types
Operator overloading allows giving special meaning to operators for user-defined types.
Show Answer & Explanation
Correct Answer: B - Function overrides base virtual function
override keyword ensures the function actually overrides a base class virtual function.
Show Answer & Explanation
Correct Answer: B - Prevents further overriding
A virtual function marked final cannot be overridden in further derived classes.
Show Answer & Explanation
Correct Answer: C - Safe downcasting with runtime check
dynamic_cast performs safe downcasting with runtime type checking for polymorphic types.
Show Answer & Explanation
Correct Answer: D - One vptr per object
Each object of a class with virtual functions has one vptr pointing to the vtable.
Show Answer & Explanation
Correct Answer: D - Type information at runtime
typeid returns type_info object containing runtime type information.
Show Answer & Explanation
Correct Answer: B - Overriding function can return derived type
Covariant return allows overriding function to return pointer/reference to derived class.
Show Answer & Explanation
Correct Answer: B - Derived function has same name but different signature
Function hiding happens when derived class has same function name but different parameters.
Show Answer & Explanation
Correct Answer: C - Runtime Type Information
RTTI (Runtime Type Information) provides type information at runtime using typeid and dynamic_cast.
Show Answer & Explanation
Correct Answer: D - Calls current class version only
Virtual functions in constructor/destructor call current class version, not derived class.
Show Answer & Explanation
Correct Answer: D - Compile-time polymorphism
Function overloading is a form of compile-time (static) polymorphism because the function to be called is determined at compile time based on the arguments.
Show Answer & Explanation
Correct Answer: C - Redefining existing operators for user-defined types
Operator overloading allows redefining the behavior of existing operators for user-defined types (classes/structs). New operators cannot be created.
Show Answer & Explanation
Correct Answer: B - :: (scope resolution)
The scope resolution operator (::), member access operator (.), sizeof, ternary operator (?:), and typeid cannot be overloaded in C++.
Show Answer & Explanation
Correct Answer: C - A function that can be overridden in a derived class for runtime polymorphism
A virtual function is a member function declared with the 'virtual' keyword in the base class. It enables runtime polymorphism by allowing derived classes to provide their own implementation.
What will be the output?
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived"; }
};
int main() {
Base* ptr = new Derived();
ptr->show();
}Show Answer & Explanation
Correct Answer: A - Derived
Because show() is virtual, the call is resolved at runtime based on the actual object type (Derived), not the pointer type (Base*). This is runtime polymorphism.
Show Answer & Explanation
Correct Answer: A - A virtual function declared with = 0
A pure virtual function is declared by assigning 0 to its declaration: virtual void func() = 0; It makes the class abstract and must be overridden in derived classes.
Show Answer & Explanation
Correct Answer: D - Compilation error
An abstract class (containing at least one pure virtual function) cannot be instantiated. Attempting to create an object of an abstract class results in a compilation error.
Show Answer & Explanation
Correct Answer: D - A lookup table of virtual function pointers used for dynamic dispatch
The VTable is a table of function pointers maintained by the compiler for each class with virtual functions. It enables dynamic dispatch by mapping virtual function calls to the correct implementation at runtime.
What will be the output?
class A {
public:
void show() { cout << "A "; }
};
class B : public A {
public:
void show() { cout << "B "; }
};
int main() {
A* p = new B();
p->show();
}Show Answer & Explanation
Correct Answer: B - A
Since show() is NOT virtual, the call is resolved based on the pointer type (A*) at compile time, not the actual object type. This is static binding, so 'A' is printed.
Show Answer & Explanation
Correct Answer: A - Complex operator+(Complex c) { ... }
The correct syntax for overloading the + operator is: return_type operator+(parameter) { ... }. The keyword 'operator' followed by the operator symbol is used.
What will be the output?
class Num {
int val;
public:
Num(int v) : val(v) {}
Num operator+(Num n) { return Num(val + n.val); }
void show() { cout << val; }
};
int main() {
Num a(3), b(7);
Num c = a + b;
c.show();
}Show Answer & Explanation
Correct Answer: C - 10
The overloaded + operator adds the val members of two Num objects. a + b creates a new Num with val = 3 + 7 = 10.
Show Answer & Explanation
Correct Answer: C - Defining multiple functions with the same name but different parameter lists
Function overloading is defining multiple functions with the same name but different parameter lists (different number, type, or order of parameters). Return type alone cannot distinguish overloaded functions.
Show Answer & Explanation
Correct Answer: A - Overloading is compile-time; overriding is runtime
Function overloading is compile-time polymorphism (same scope, different parameters). Function overriding is runtime polymorphism (different scope via inheritance, same signature with virtual).
What will be the output?
class Shape {
public:
virtual double area() = 0;
};
class Circle : public Shape {
double r;
public:
Circle(double r) : r(r) {}
double area() override { return 3.14 * r * r; }
};
int main() {
Shape* s = new Circle(5);
cout << s->area();
}Show Answer & Explanation
Correct Answer: C - 78.5
Shape is abstract with pure virtual area(). Circle overrides it with 3.14 * 5 * 5 = 78.5. The virtual function call resolves to Circle::area() at runtime.
Show Answer & Explanation
Correct Answer: B - override
The 'override' keyword (C++11) explicitly indicates that a function is intended to override a virtual function in the base class. It generates a compiler error if no matching virtual function exists.
Show Answer & Explanation
Correct Answer: A - No
Constructors cannot be virtual in C++. The virtual mechanism relies on the VTable pointer which is set up during construction, so virtual constructors would be a circular dependency.
Show Answer & Explanation
Correct Answer: B - To prevent memory leaks during polymorphic deletion
When deleting a derived object through a base class pointer, a non-virtual destructor will only call the base destructor, causing memory leaks. A virtual destructor ensures the correct destructor chain is called.
What will be the output?
class Base {
public:
virtual ~Base() { cout << "~Base "; }
};
class Derived : public Base {
public:
~Derived() { cout << "~Derived "; }
};
int main() {
Base* p = new Derived();
delete p;
}Show Answer & Explanation
Correct Answer: C - ~Derived ~Base
Because the destructor is virtual, deleting through the base pointer correctly calls ~Derived first, then ~Base. Output: '~Derived ~Base'.
Show Answer & Explanation
Correct Answer: D - Resolving function calls at runtime using virtual functions
Late binding (dynamic binding) is the process of resolving function calls at runtime, typically through virtual functions and the VTable mechanism. This enables runtime polymorphism.
Show Answer & Explanation
Correct Answer: A - friend ostream& operator<<(ostream& os, const MyClass& obj)
The << operator for output is overloaded as a friend function taking an ostream reference and a const reference to the class object, returning an ostream reference for chaining.
What will be the output?
void print(int x) { cout << "int: " << x; }
void print(double x) { cout << "double: " << x; }
int main() {
print(5);
}Show Answer & Explanation
Correct Answer: A - int: 5
The literal 5 is an int, so the compiler selects print(int x) as the best match. This is compile-time polymorphism through function overloading.
What will be the output?
class Base {
public:
virtual void func(int x = 10) { cout << "Base: " << x; }
};
class Derived : public Base {
public:
void func(int x = 20) override { cout << "Derived: " << x; }
};
int main() {
Base* p = new Derived();
p->func();
}Show Answer & Explanation
Correct Answer: A - Derived: 10
Virtual functions use dynamic binding for the function body but default arguments use static binding (based on pointer type). So Derived::func is called but with Base's default argument 10.
Show Answer & Explanation
Correct Answer: B - To prevent further overriding in derived classes
The 'final' specifier (C++11) prevents a virtual function from being overridden in further derived classes. Any attempt to override a final function results in a compilation error.
What will be the output?
class A {
public:
virtual void show() { cout << "A "; }
};
class B : public A {
public:
void show() override { cout << "B "; }
};
class C : public B {
public:
void show() override { cout << "C "; }
};
int main() {
A* p = new C();
p->show();
}Show Answer & Explanation
Correct Answer: A - C
With virtual functions, the call is resolved based on the actual object type at runtime. The object is of type C, so C::show() is called, printing 'C'.
Show Answer & Explanation
Correct Answer: B - A hidden pointer in each object that points to the class's VTable
The VPTR is a hidden pointer automatically added by the compiler to each object of a class with virtual functions. It points to the class's VTable for dynamic dispatch.
Show Answer & Explanation
Correct Answer: C - Yes, but only as a member function
The assignment operator can be overloaded but only as a member function of the class. It cannot be overloaded as a non-member or friend function.
Show Answer & Explanation
Correct Answer: D - Early binding happens at compile time; late binding at runtime
Early binding (static binding) resolves function calls at compile time (e.g., function overloading). Late binding (dynamic binding) resolves at runtime using the VTable mechanism (e.g., virtual functions).
What will be the output?
class A {
public:
void show() { cout << "A"; }
virtual void display() { cout << "A"; }
};
class B : public A {
public:
void show() { cout << "B"; }
void display() override { cout << "B"; }
};
int main() {
A* p = new B();
p->show();
p->display();
}Show Answer & Explanation
Correct Answer: A - AB
show() is non-virtual, so it uses static binding (A* type) printing 'A'. display() is virtual, so it uses dynamic binding (B object) printing 'B'. Output: 'AB'.
Show Answer & Explanation
Correct Answer: D - They can have constructors
Abstract classes can have constructors (called when derived objects are created), data members, and non-virtual functions. They just cannot be instantiated directly due to pure virtual functions.
Show Answer & Explanation
Correct Answer: C - Larger due to the VPTR
A class with virtual functions has a hidden VPTR (virtual pointer) added to each object, which typically adds 4 or 8 bytes (depending on the platform) to the object size.