This set of Object Oriented Programming using C++ Questions and Answers for Entrance exams focuses on “Virtual Functions”.
1. Virtual function is ______ class function which expected to be redefined in ______ class, so that when reference is made to derived class object using pointer then we can call virtual function to execute ________ class definition version.
a) Base, derived, derived
b) Derived, Derived, Derived
c) Base, derived, base
d) Base, base, derived
Explanation: The functions which may give rise to ambiguity due to inheritance, can be declared virtual. So that whenever derived class object is referred using pointer or reference to the base class methods, we can still call the derived class methods using virtual function. Hence this differentiates those methods from each other.
2. What does a virtual function ensure for an object, among the following?
a) Correct method is called, regardless of the class defining it
b) Correct method is called, regardless of the object being called
c) Correct method is called, regardless of the type of reference used for function call
d) Correct method is called, regardless of the type of function being called by objects
Explanation: It is property of the virtual function and one of their main use. Its use ensure that the correct method is called even though it is been called from different pointer or references. This also decreases chance of mistakes in program.
3. Virtual functions are mainly used to achieve _____________
a) Compile time polymorphism
b) Interpreter polymorphism
c) Runtime polymorphism
d) Functions code polymorphism
Explanation: It is used to achieve runtime polymorphism. The functions which are inherited and overridden, so at runtime the correct function is executed. The correct function call is made from the intended class.
4. Which keyword is used to declare virtual functions?
Explanation: The virtual keyword is used to declare virtual functions. Anonymous keyword is used with classes and have a different meaning. The virtual functions are used to call the intended function of the derived class.
5. Where the virtual function should be defined?
a) Twice in base class
b) Derived class
c) Base class and derived class
d) Base class
Explanation: The virtual function should be declared in base class. So that when the derived class inherits from the base class, the functions can be differentiated from the one in base class and another in derived class.
6. The resolving of virtual functions is done at ______________
a) Compile time
b) Interpret time
d) Writing source code
Explanation: The resolving of virtual functions that are to be called is done at run time. The base class and the derived classes may contain different definitions and different variables, so all these things are resolved at run time and decided which function is to be called.
7. In which access specifier should a virtual function be defined?
Explanation: The virtual functions must be defined in public section of a class. This is to ensure that the virtual function is available everywhere in the program. Also to avoid any error while resolving the method.
8. Virtual functions can never be made _______________
a) Static function
b) Parameterized function
c) Default argument function
d) Zero parameter function
Explanation: The virtual function must not be static. Those functions are the property of individual objects and not of a class as a whole. The functions should not be made common for all the objects of that class.
9. Virtual functions can’t be made friend function of other classes.
Explanation: The friend functions can access the private members also. This may hinder the security of class members. This is why the functions should not be made friend functions of other class.
10. Which is a must condition for virtual function to achieve runtime polymorphism?
a) Virtual function must be accessed with direct name
b) Virtual functions must be accessed using base class object
c) Virtual function must be accessed using pointer or reference
d) Virtual function must be accessed using derived class object only
Explanation: The virtual functions must be called using pointer or reference. This is mandatory so that the intended function gets executed while resolving the method at runtime. The must not be any ambiguity between the method of parent class and derived class.
11. Which among the following is true for virtual functions?
a) Prototype must be different in base and derived class
b) Prototype must be same in base class and derived class
c) Prototype must be given only in base class
d) Prototype must have different signature in base and derived class
Explanation: The prototype must be the same. Because the function is to be overridden in the derived class. If the function prototype is different in derived class then it will not override the base class function and hence virtual function concept won’t work here.
12. The virtual functions must be declared and defined in _____________ class and overridden in ___________ class.
a) Base, base
b) Derived, derived
c) Derived, base
d) Base, derived
Explanation: The virtual functions must be declared and defined in base class. The functions can be redefined in derived class. If redefined in derived class then it overrides the base class function definition.
13. It is __________ to redefine the virtual function in derived class.
b) Not necessary
c) Not acceptable
d) Good practice
Explanation: It is not necessary to redefine the virtual function in the derived class. If not defined, the base class function definition is used but if defined, the intended definition is used according to need. It is not about good coding practice as it should be redefined only if needed.
14. Which among the following is true?
a) A class may have virtual destructor but not virtual constructor
b) A class may have virtual constructor but not virtual destructor
c) A class may have virtual constructor and virtual constructor
d) A class may have either virtual destructor or virtual constructor
Explanation: Any class can contain virtual destructor. But is not possible to define a virtual constructor. The reason behind is that the destructors can be overridden but constructors should not be.
15. If virtual function of base class is redefined in derived class then ________________
a) It must be declared virtual in derived class also
b) It may or may not be declared virtual in derived class
c) It can must not be declared virtual in derived class
d) It must be declared normally in derived class
Explanation: The virtual functions may or may not be declared virtual in derived class. This is because if the overriding function defined in derived class is not declared virtual explicitly, the compiler makes it virtual implicitly.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming using C++ for Entrance exams, here is complete set of 1000+ Multiple Choice Questions and Answers.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!