250+ TOP MCQs on Virtual Functions and Answers

Object Oriented Programming Questions Entrance exams 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: c
Clarification: 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?
a) virtual
b) virt
c) anonymous
d) virtually
Answer: a
Clarification: 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
Answer: d
Clarification: 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
c) Runtime
d) Writing source code
Answer: c
Clarification: 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?
a) Private
b) Public
c) Protected
d) Default
Answer: b
Clarification: 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
Answer: a
Clarification: 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.
a) True
b) False
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: d
Clarification: 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.
a) Necessary
b) Not necessary
c) Not acceptable
d) Good practice
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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.

Object Oriented Programming for Entrance exams,

Leave a Reply

Your email address will not be published. Required fields are marked *