Back to Practice OOP with C++

Polymorphism - Practice MCQs for CCAT

50 Questions Section B: Programming OOP with C++

Polymorphism Question Bank for C-CAT

Topic-wise Polymorphism MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Compile-time polymorphism is achieved by:
AFunction overloading
BVirtual functions
CDynamic binding
DBase class pointer
Show Answer & Explanation

Correct Answer: A - Function overloading

Function overloading is resolved at compile time (static polymorphism).

Q2.
Runtime polymorphism requires:
AVirtual functions
BOperator overloading
CFunction overloading
DStatic functions
Show Answer & Explanation

Correct Answer: A - Virtual functions

Virtual functions enable runtime polymorphism through dynamic binding.

Q3.
Pure virtual function syntax:
Avirtual void f()
Bvoid f() = 0
Cvirtual void f() = 0
Dpure virtual void f()
Show Answer & Explanation

Correct Answer: C - virtual void f() = 0

Pure virtual function is declared with = 0.

Q4.
Abstract class has:
AAt least one pure virtual function
BNo functions
COnly static functions
DNo data members
Show Answer & Explanation

Correct Answer: A - At least one pure virtual function

A class with at least one pure virtual function is abstract.

Q5.
Abstract class can be:
AInstantiated
BOnly inherited
CDestroyed
DCopied
Show Answer & Explanation

Correct Answer: B - Only inherited

Abstract classes cannot be instantiated, only inherited.

Q6.
Function overloading is based on:
AReturn type only
BParameter list
CFunction name only
DAccess specifier
Show Answer & Explanation

Correct Answer: B - Parameter list

Overloading is resolved by different parameter list (number/type).

Q7.
Operator overloading cannot overload:
A+
B[]
C<<
D::
Show Answer & Explanation

Correct Answer: D - ::

Scope resolution operator (::) cannot be overloaded.

Q8.
Virtual destructor is needed when:
AAlways
BNever
COnly with multiple inheritance
DUsing base pointer to delete derived object
Show Answer & Explanation

Correct Answer: D - Using base pointer to delete derived object

Virtual destructor ensures proper cleanup when deleting via base pointer.

Q9.
vptr is created for:
AEvery class
BStatic classes
CClasses with virtual functions
DFriend classes
Show Answer & Explanation

Correct Answer: C - Classes with virtual functions

Virtual pointer (vptr) is created for classes with virtual functions.

Q10.
Late binding occurs at:
ACompile time
BLoad time
CLink time
DRuntime
Show Answer & Explanation

Correct Answer: D - Runtime

Late binding (dynamic binding) occurs at runtime.

Q11.
What is operator overloading?
ADefining new operators
BGiving operators new meaning for user types
CRemoving operators
DChanging operator precedence
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.

Q12.
override keyword ensures:
AFunction is new
BFunction overrides base virtual function
CFunction is static
DFunction is inline
Show Answer & Explanation

Correct Answer: B - Function overrides base virtual function

override keyword ensures the function actually overrides a base class virtual function.

Q13.
final keyword on virtual function:
AMakes it pure virtual
BPrevents further overriding
CMakes it static
DRemoves from vtable
Show Answer & Explanation

Correct Answer: B - Prevents further overriding

A virtual function marked final cannot be overridden in further derived classes.

Q14.
Dynamic_cast is used for:
AStatic type conversion
BRemoving const
CSafe downcasting with runtime check
DBit-level casting
Show Answer & Explanation

Correct Answer: C - Safe downcasting with runtime check

dynamic_cast performs safe downcasting with runtime type checking for polymorphic types.

Q15.
Size overhead of virtual function:
ANo overhead
BDoubles class size
COne pointer per function
DOne vptr per object
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.

Q16.
typeid operator returns:
AObject size
BCompilation error
CObject address
DType information at runtime
Show Answer & Explanation

Correct Answer: D - Type information at runtime

typeid returns type_info object containing runtime type information.

Q17.
Covariant return type means:
ASame return type always
BOverriding function can return derived type
CNo return value
DMultiple return types
Show Answer & Explanation

Correct Answer: B - Overriding function can return derived type

Covariant return allows overriding function to return pointer/reference to derived class.

Q18.
Function hiding occurs when:
AVirtual function is overridden
BDerived function has same name but different signature
CFunction is private
DFunction is static
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.

Q19.
RTTI stands for:
AReturn Type Type Info
BReal Time Type Interface
CRuntime Type Information
DReference To Type Instance
Show Answer & Explanation

Correct Answer: C - Runtime Type Information

RTTI (Runtime Type Information) provides type information at runtime using typeid and dynamic_cast.

Q20.
Virtual function in constructor:
AWorks polymorphically
BNot allowed
CCauses error
DCalls current class version only
Show Answer & Explanation

Correct Answer: D - Calls current class version only

Virtual functions in constructor/destructor call current class version, not derived class.

Q21.
Which type of polymorphism is achieved through function overloading?
ARuntime polymorphism
BVirtual polymorphism
CDynamic polymorphism
DCompile-time polymorphism
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.

Q22.
What is operator overloading in C++?
ADefining new operators
BRemoving operators from a class
CRedefining existing operators for user-defined types
DMaking operators work faster
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.

Q23.
Which of the following operators cannot be overloaded in C++?
A+ (addition)
B:: (scope resolution)
C[] (subscript)
D<< (insertion)
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++.

Q24.
What is a virtual function?
AA function that doesn't exist
BA function that is always inline
CA function that can be overridden in a derived class for runtime polymorphism
DA static function in a base class
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.

Q25.

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();
}
ADerived
BBase
CBaseDerived
DCompilation error
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.

Q26.
What is a pure virtual function?
AA virtual function declared with = 0
BA virtual function with an empty body
CA function that cannot be overridden
DA function with only declaration
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.

Q27.
What will happen if you try to instantiate an abstract class?
AObject with default values is created
BObject with null values is created
CRuntime error
DCompilation error
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.

Q28.
What is the VTable (Virtual Table)?
AA table of all variables in a class
BA table of static functions
CA table of constructors
DA lookup table of virtual function pointers used for dynamic dispatch
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.

Q29.

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();
}
AB
BA
CAB
DCompilation error
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.

Q30.
Which of the following correctly overloads the + operator for a class Complex?
AComplex operator+(Complex c) { ... }
BComplex +(Complex c) { ... }
Coperator Complex+(Complex c) { ... }
DComplex add(Complex c) { ... }
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.

Q31.

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();
}
A3
B7
C10
DCompilation error
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.

Q32.
What is function overloading?
ADefining multiple functions with the same name but different return types only
BDefining a function inside another function
CDefining multiple functions with the same name but different parameter lists
DRedefining a base class function in derived class
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.

Q33.
What is the difference between function overloading and function overriding?
AOverloading is compile-time; overriding is runtime
BOverloading requires inheritance; overriding doesn't
CThey are the same thing
DOverloading requires virtual; overriding doesn't
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).

Q34.

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();
}
A0
B25
C78.5
DCompilation error
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.

Q35.
What keyword in C++11 ensures that a function is correctly overriding a virtual function?
Avirtual
Boverride
Cfinal
Dexplicit
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.

Q36.
Can constructors be virtual in C++?
ANo
BYes
COnly in abstract classes
DOnly with pure virtual syntax
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.

Q37.
Why should base class destructors be virtual when using polymorphism?
ATo make construction faster
BTo prevent memory leaks during polymorphic deletion
CTo allow constructor overloading
DVirtual destructors are optional
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.

Q38.

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;
}
A~Base
B~Derived
C~Derived ~Base
D~Base ~Derived
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'.

Q39.
What is late binding (dynamic binding)?
AResolving function calls at compile time
BLinking libraries at load time
CBinding variables to types at declaration
DResolving function calls at runtime using virtual functions
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.

Q40.
Which of the following correctly overloads the << operator for a class?
Afriend ostream& operator<<(ostream& os, const MyClass& obj)
Bvoid operator<<(MyClass& obj)
CMyClass operator<<(ostream& os)
Dstatic ostream& operator<<(ostream& os, MyClass obj)
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.

Q41.

What will be the output?

void print(int x) { cout << "int: " << x; }
void print(double x) { cout << "double: " << x; }
int main() {
  print(5);
}
Aint: 5
Bdouble: 5
CCompilation error (ambiguity)
Ddouble: 5.0
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.

Q42.

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();
}
ADerived: 10
BDerived: 20
CBase: 10
DBase: 20
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.

Q43.
What is the 'final' specifier used for with virtual functions?
ATo make a function pure virtual
BTo prevent further overriding in derived classes
CTo make a function static
DTo delete a function
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.

Q44.

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();
}
AC
BB
CA
DABC
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'.

Q45.
What is the VPTR (Virtual Pointer)?
AA pointer to the base class
BA hidden pointer in each object that points to the class's VTable
CA pointer to virtual memory
DA pointer to the destructor
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.

Q46.
Can you overload the assignment operator (=) in C++?
ANo, it's a built-in operator
BYes, both as member and non-member
CYes, but only as a member function
DYes, but only as a friend function
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.

Q47.
What is the difference between early binding and late binding?
AEarly binding is slower; late binding is faster
BEarly binding uses VTable; late binding doesn't
CThey are the same mechanism
DEarly binding happens at compile time; late binding at runtime
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).

Q48.

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();
}
AAB
BBB
CAA
DBA
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'.

Q49.
Which of the following is true about abstract classes?
AThey cannot have data members
BThey can be instantiated directly
CThey cannot have non-virtual functions
DThey can have constructors
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.

Q50.
What is the output of sizeof for a class with virtual functions compared to the same class without?
ASame size
BSmaller due to optimization
CLarger due to the VPTR
DDepends on the compiler only
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.

Showing 1-10 of 50 questions