Virtual Functions in C ++ Programming MCQ Questions and Answers

1. Which keyword in C++ is used to declare a virtual function?
A) static
B) virtual
C) abstract
D) override
Answer: B

2. Virtual functions are primarily used to achieve:
A) Compile-time polymorphism
B) Run-time polymorphism
C) Function overloading
D) Template specialization
Answer: B

3. A virtual function must be declared inside a:
A) Global scope
B) Struct only
C) Class or struct
D) Namespace
Answer: C

4. If a base class pointer points to a derived class object, calling a virtual function invokes:
A) Base class version
B) Derived class version
C) Compiler-generated version
D) Undefined behavior
Answer: B

5. Which of the following cannot be declared as a virtual function?
A) Member function
B) Constructor
C) Destructor
D) Overridden method
Answer: B

6. A pure virtual function is declared by:
A) Using virtual void f() = 0;
B) Using virtual void f() = 0;
C) Using void f() = virtual;
D) Using pure virtual f();
Answer: B

7. A class containing at least one pure virtual function is known as:
A) Virtual class
B) Abstract class
C) Dynamic class
D) Polymorphic class
Answer: B

8. Which table is created internally by the compiler to support virtual functions?
A) Heap table
B) Memory map
C) Virtual table (vtable)
D) Object table
Answer: C

9. Virtual table entries contain:
A) Names of functions
B) Return types
C) Function pointers
D) Argument counts
Answer: C

10. A pure virtual function must be:
A) Defined inside the base class only
B) Defined outside the class only
C) Overridden in derived class
D) Declared as static
Answer: C

11. If a derived class does not override a pure virtual function, that class becomes:
A) Concrete class
B) Abstract class
C) Static class
D) Final class
Answer: B

12. When a virtual function is called through a base class reference, resolution occurs:
A) At compile time
B) At runtime
C) During linking
D) At template instantiation
Answer: B

13. Virtual functions in C++ are implemented using:
A) Inline expansion
B) Operator overloading
C) Function pointers in vtable
D) Template mechanism
Answer: C

14. Which of the following statements about virtual destructors is true?
A) They are optional and not recommended
B) They ensure correct destructor call order
C) They are used for operator overloading
D) They must be pure virtual
Answer: B

15. If a virtual function is not overridden in a derived class:
A) Compilation error occurs
B) Base class version is used
C) Program crashes
D) Function call is ignored
Answer: B

16. The process of determining the correct function to call for a virtual function is known as:
A) Function overloading
B) Early binding
C) Late binding
D) Function overriding
Answer: C

17. A class with only pure virtual functions and no data members can act as:
A) Singleton
B) Template class
C) Interface
D) Friend class
Answer: C

18. Which of the following can have a virtual destructor?
A) Struct only
B) Namespace
C) Class
D) Union
Answer: C

19. The address of the virtual table pointer is stored:
A) In global memory
B) In stack
C) Inside each object of the class
D) In static storage
Answer: C

20. The function overriding mechanism through virtual functions supports:
A) Encapsulation
B) Polymorphism
C) Abstraction only
D) Inheritance only
Answer: B

21. Declaring a destructor as virtual in the base class ensures:
A) Faster execution
B) Static memory allocation
C) Proper cleanup of derived objects
D) Elimination of vtable
Answer: C

22. The keyword override in C++11 ensures that:
A) The base class function is virtual
B) A derived function correctly overrides a base function
C) Virtual table is updated
D) Memory is deallocated
Answer: B

23. A class with at least one virtual function will have:
A) One vtable shared by all classes
B) One vtable per class
C) One vtable per object
D) No vtable at all
Answer: B

24. Which of the following statements about pure virtual destructors is true?
A) They cannot have a definition
B) They must have a definition
C) They are optional
D) They cannot be virtual
Answer: B

25. If a base class pointer calls a non-virtual function implemented in derived class:
A) Derived version is called
B) Base version is called
C) Causes ambiguity
D) Runtime error occurs
Answer: B

26. Which of the following is true about virtual function overriding?
A) The base and derived function must differ in parameters
B) A virtual function can be overridden by a static function
C) The base and derived functions must have the same signature
D) Virtual functions cannot be overridden
Answer: C

27. When a virtual function is called inside a constructor, which function executes?
A) Derived class version
B) Base class version
C) Both base and derived versions
D) Causes runtime error
Answer: B

28. If a class contains a virtual function, its size generally increases due to:
A) Extra data members
B) Template expansion
C) Addition of vtable pointer
D) Overloaded constructors
Answer: C

29. A virtual function can be defined as:
A) A function declared with the keyword inline
B) A static function inside a class
C) A member function declared with virtual keyword
D) A friend function
Answer: C

30. The concept of dynamic binding in C++ is implemented through:
A) Templates
B) Virtual functions
C) Function overloading
D) Operator overloading
Answer: B

31. Which of the following statements is false about virtual functions?
A) They enable runtime polymorphism
B) They can be redefined in derived classes
C) They can be constructors
D) They can be overridden
Answer: C

32. A class derived from an abstract class must:
A) Define all its own functions only
B) Override the constructor
C) Implement all pure virtual functions
D) Have a virtual table
Answer: C

33. Which keyword is used in C++11 to prevent a derived class from overriding a virtual function?
A) override
B) finalclass
C) final
D) virtualfinal
Answer: C

34. Virtual function calls are resolved using:
A) Early binding
B) Static binding
C) Dynamic binding
D) Template instantiation
Answer: C

35. Which of the following can be a virtual function?
A) Static function
B) Friend function
C) Non-static member function
D) Inline function only
Answer: C

36. If a virtual function is defined in the base class but not overridden in the derived class, and is called through a derived object pointer, it executes:
A) Undefined behavior
B) Derived class version
C) Base class version
D) Compilation error
Answer: C

37. What is the return type of a virtual function?
A) Must always be void
B) Must always be int
C) Can be any valid type
D) Must match the base class’s constructor
Answer: C

38. What will happen if you try to instantiate an abstract class directly?
A) It will compile successfully
B) It will link successfully
C) It will cause a compilation error
D) It will execute base functions
Answer: C

39. A derived class overriding a virtual function must have:
A) Same name only
B) Same name, return type, and parameters
C) Same name but different parameters
D) Same scope
Answer: B

40. Which of the following is created by the compiler for classes with virtual functions?
A) Static map
B) vtable and vptr
C) Heap allocator
D) Template type list
Answer: B

41. The virtual function mechanism supports which OOP concept most directly?
A) Data hiding
B) Abstraction
C) Polymorphism
D) Inheritance only
Answer: C

42. The override specifier helps the compiler to:
A) Increase performance
B) Generate new vtable
C) Detect mismatched function signatures
D) Prevent base access
Answer: C

43. Which of the following functions can be declared virtual?
A) Friend function
B) Constructor
C) Destructor
D) Template specialization
Answer: C

44. Virtual functions cannot be:
A) Public
B) Protected
C) Static
D) Private
Answer: C

45. When using multiple inheritance, each base class with virtual functions contributes:
A) One shared vtable for all
B) One vtable per base class
C) No vtable
D) vtable only for derived
Answer: B

46. The performance overhead of virtual functions is primarily due to:
A) Large memory consumption
B) Indirection through vtable
C) Template expansion
D) Compiler optimizations
Answer: B

47. Which type of binding occurs with non-virtual functions?
A) Dynamic binding
B) Static binding
C) Late binding
D) Run-time binding
Answer: B

48. Which operator cannot be overloaded as a virtual function?
A) +
B) []
C) :: (scope resolution)
D) =
Answer: C

49. The keyword virtual can be used with which of the following member functions?
A) Static only
B) Non-static only
C) Global functions
D) Friend functions
Answer: B

50. A virtual function is dynamically bound only when it is called:
A) Directly through object name
B) Using this pointer
C) Through a base class pointer or reference
D) Inside the constructor
Answer: C

51. What happens if a class has a virtual destructor but no virtual functions?
A) Compilation error occurs
B) It becomes an abstract class
C) It still maintains a vtable
D) Destructor becomes non-functional
Answer: C

52. Can a virtual function be private in a class?
A) No, only public functions can be virtual
B) Yes, it can be private
C) Only protected functions can be virtual
D) Only static members can be virtual
Answer: B

53. Which of the following cannot be declared as pure virtual?
A) Destructor
B) Member function
C) Constructor
D) Operator function
Answer: C

54. If a derived class does not implement a pure virtual function, then:
A) It becomes concrete
B) It can still be instantiated
C) It remains abstract
D) It loses its inheritance
Answer: C

55. The term “virtual” in C++ refers to:
A) Logical memory
B) Dynamic allocation
C) Late binding of functions
D) File system abstraction
Answer: C

56. Which function call uses the vtable mechanism?
A) Non-member function
B) Virtual member function
C) Inline function
D) Static function
Answer: B

57. What is the output behavior when a derived object is deleted through a base pointer without a virtual destructor?
A) Both destructors are called
B) Only derived destructor is called
C) Only base destructor is called
D) Compilation error occurs
Answer: C

58. Which of the following best defines a pure virtual function?
A) A function with no parameters
B) A function without return type
C) A function with declaration but no definition in base class
D) A friend function
Answer: C

59. When a virtual function is called through an object (not pointer/reference), binding occurs:
A) At runtime
B) At compile time
C) During linking
D) Undefined
Answer: B

60. Virtual functions allow derived classes to:
A) Delete base class data
B) Redefine inherited behavior
C) Access private members
D) Avoid constructors
Answer: B

61. Which of the following is true about vtable and vptr?
A) vptr is common for all objects
B) Each object has its own vptr pointing to the class vtable
C) vptr and vtable are global
D) vtable changes per object
Answer: B

62. Which access specifier can be used for virtual functions?
A) Only public
B) Only protected
C) Any access specifier
D) Only private
Answer: C

63. What is the type of variable vptr used in virtual function implementation?
A) Static array
B) Object
C) Hidden pointer maintained by compiler
D) Function
Answer: C

64. The presence of a virtual function in a base class means:
A) The base class must be abstract
B) The class cannot be inherited
C) The class is polymorphic
D) The class must override it
Answer: C

65. Which of the following statements about pure virtual destructors is true?
A) They can’t have a body
B) They must have a body
C) They can’t be declared
D) They cannot be inherited
Answer: B

66. Which C++ mechanism ensures that the correct function is invoked when a virtual function is called through a base pointer?
A) Compiler optimization
B) Dynamic dispatch
C) Function overloading
D) Inline substitution
Answer: B

67. What is a “polymorphic class” in C++?
A) A class with multiple constructors
B) A class with at least one virtual function
C) A class with multiple inheritance
D) A class with templates
Answer: B

68. Virtual functions are resolved:
A) At compile time using name mangling
B) At runtime using vtable
C) During linking
D) Using inline expansion
Answer: B

69. Can we make a pure virtual function have a definition outside the class?
A) No, never
B) Yes, it can have a definition outside
C) Only if static
D) Only in derived class
Answer: B

70. What is the correct syntax for a pure virtual function in C++?
A) virtual int func();
B) virtual int func() = 0;
C) int virtual func() = 0;
D) pure virtual int func();
Answer: B

71. When a class has a virtual function, what changes internally?
A) Object’s layout remains same
B) Object includes a hidden vptr pointer
C) Only derived classes change
D) Memory allocation fails
Answer: B

72. Which type of inheritance most commonly uses virtual functions?
A) Multiple inheritance
B) Single inheritance
C) Hierarchical inheritance
D) Multilevel inheritance
Answer: C

73. Which of the following best defines overriding?
A) Reusing base class constructor
B) Redefining a base class virtual function in derived class
C) Overloading a base class function
D) Changing parameter count
Answer: B

74. What will happen if you call a pure virtual function from a constructor?
A) It executes derived version
B) It executes base version
C) It causes undefined behavior
D) It calls virtual destructor
Answer: C

75. Which of the following operators cannot be made virtual?
A) ->
B) ()
C) :: (scope resolution)
D) []
Answer: C

76. When a virtual function is overridden in a derived class, what happens to the vtable entry?
A) A new vtable is created for base class
B) The entry is deleted
C) The vtable entry is replaced with the derived function address
D) The vtable remains unchanged
Answer: C

77. Which of the following is not true about virtual functions?
A) They support late binding
B) They can be static
C) They can be overridden
D) They are resolved at runtime
Answer: B

78. A virtual destructor ensures that:
A) The base destructor is skipped
B) Both base and derived destructors execute in proper order
C) Only the base destructor executes
D) No destructor executes
Answer: B

79. Which of the following cannot be declared virtual in C++?
A) Destructor
B) Constructor
C) Assignment operator
D) Member function
Answer: B

80. The = 0 in a virtual function declaration indicates:
A) Inline expansion
B) Pure virtual declaration
C) Static binding
D) Virtual destructor
Answer: B

81. Which of the following concepts is implemented using virtual functions?
A) Function overloading
B) Operator overloading
C) Runtime polymorphism
D) Static dispatch
Answer: C

82. Which of the following statements is true for a base class with virtual functions?
A) It can’t be instantiated
B) It can be instantiated unless it has pure virtuals
C) It must be abstract
D) It must have only protected members
Answer: B

83. When you delete an object through a base class pointer with a non-virtual destructor, it leads to:
A) Safe memory release
B) Undefined behavior
C) Compile-time error
D) Partial cleanup
Answer: B

84. Which type of memory structure supports vtable storage in most C++ implementations?
A) Stack memory
B) Static memory segment
C) Heap memory
D) File system
Answer: B

85. What happens if the base class defines a pure virtual destructor but no definition is provided?
A) Works fine
B) No destructor is called
C) Linker error occurs
D) Program crashes at runtime
Answer: C

86. If a derived class hides a base class virtual function (by changing parameters), then calling it via base pointer will:
A) Call derived version
B) Call base version
C) Cause compilation error
D) Call both
Answer: B

87. Which of the following is created automatically when a virtual function is declared in a class?
A) Template class
B) Derived object
C) vtable and vptr setup
D) Interface object
Answer: C

88. What will happen if a virtual function is called in the base class constructor and overridden in derived class?
A) Derived function executes
B) Base class version executes
C) Causes recursion
D) Undefined behavior
Answer: B

89. Virtual function tables are generated:
A) At runtime
B) At compile time
C) During linking
D) During program execution
Answer: B

90. What happens when a class has a pure virtual function but no derived class overrides it?
A) Base class executes
B) Derived class executes default version
C) Derived class remains abstract
D) Compilation warning
Answer: C

91. Which of the following can you use to call a base version of a virtual function explicitly?
A) Direct object call
B) Virtual pointer
C) Scope resolution operator (::)
D) Dynamic cast
Answer: C

92. Which statement is true about overriding a virtual function?
A) The overridden function must be static
B) The base function must not be virtual
C) The signatures must match exactly
D) The derived function must have a different return type
Answer: C

93. The keyword virtual affects which of the following aspects of a member function?
A) Memory allocation
B) Binding time
C) Access level
D) Parameter type
Answer: B

94. If two base classes have virtual functions with the same signature and a derived class overrides one, what happens?
A) Compilation fails
B) Both base vtables get updated
C) Only one vtable is used
D) Derived hides both versions
Answer: B

95. A pure virtual function must be defined:
A) In base class header
B) Inside constructor
C) In derived class
D) In friend function
Answer: C

96. When a class inherits virtually from a base class, how does it affect virtual functions?
A) They become static
B) They are removed from vtable
C) They are shared across subobjects
D) They lose polymorphism
Answer: C

97. Which of the following operations cannot be virtual?
A) Operator []
B) Operator ()
C) Operator ::
D) Operator ->
Answer: C

98. A function can be both virtual and inline. However, inline expansion happens:
A) Always
B) Only when called statically
C) Even for runtime calls
D) For pure virtuals
Answer: B

99. Which of the following indicates that a class is abstract?
A) It has a constructor
B) It is inherited
C) It has at least one pure virtual function
D) It uses templates
Answer: C

100. Which of the following statements is correct regarding virtual inheritance and virtual functions?
A) They are unrelated mechanisms
B) Virtual inheritance disables virtual functions
C) Both are independent but can coexist
D) Virtual functions require virtual inheritance
Answer: C