MCQs – C++ Virtual Concepts

(Last Updated On: May 9, 2018)

MCQ on virtual function, VPTR, VTABLE and destructors etc. for interview exams preparation. These multiple-choice questions on virtual concepts contain the answer and explanation.


Q) Which concept is not available in C++?

  1. Virtual constructor
  2. Virtual destructor
  3. Virtual function
  4. Virtual Table
View Answer
Answer: A
There is no concept of virtual constructor in C++ programming. Read why virtual constructor is not possible in C++.


Q) What is output of the following C++ program?


  1. A::f()
  2. A::f() A::f()
  3. A::f() A::f()
  4. Compiler error
View Answer
Answer: D
Explanation: The above class represent the virtual base class / diamond problem in C++. The object D will try to access the function f() of base class via class B and class C. as both B and C inherites A. This will lead to ambiguity call the function.
So, compiler will flash an error. i.e. ambiguous access of f().
If we make the class B and C virtual like below, then there would not be any ambiguity and program will work fine.


Q) What is purpose of Virtual destructor?

    1. To maintain the call hierarchy of destructors of base and derived classes.
    2. Destructor can be virtual so that we can override the destructor of base class in derived class.
    3. Virtual destructor is to destroy the VTable
    4. None
View Answer
Answer: A
Base class destructor is made virtual in inheritance relationship to maintain the call of destructors in base and derived class. Note that first derived class destructor will be called then derived class. Read execution of order of constructors and destructors in C++. Also, Read why and when virtual destructor is useful ?


Q) When VTABLE (Virtual table) get created for a class?

  1. Every class by default has virtual table
  2. When a Class Overrides the function of Base class
  3. When a class contains at least one virtual function.
  4. When a class is derived from a base class.
View Answer
Answer: C

Q) Number of VTable created for following C++ program is ________

  1. 0
  2. 1
  3. 3
  4. 4
View Answer
Answer: B
Compiler will create only 1 virtual table for the class. Virtual table does not depend upon number of objects created.