Test Series - oops

Test Number 47/78

Q: Virtual functions can never be made _______________
A. Static function
B. Parameterized function
C. Default argument function
D. Zero parameter function
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: It is __________ to redefine the virtual function in derived class.
A. Necessary
B. Not necessary
C. Not acceptable
D. Good practice
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which among the following best defines the abstract methods?
A. Functions declared and defined in base class
B. Functions only declared in base class
C. Function which may or may not be defined in base class
D. Function which must be declared in derived class
Solution: The abstract functions must only be declared in base class. Their definitions are provided by the derived classes. It is a mandatory condition.
Q: Which among the following is true?
A. The abstract functions must be only declared in derived classes
B. The abstract functions must not be defined in derived classes
C. The abstract functions must be defined in base and derived class
D. The abstract functions must be defined either in base or derived class
Solution: The abstract functions can’t be defined in base class. They are to be defined in derived classes. It is a rule for abstract functions.
Q: How are abstract functions different from the abstract functions?
A. Abstract must not be defined in base class whereas virtual function can be defined
B. Either of those must be defined in base class
C. Different according to definition
D. Abstract functions are faster
Solution: The abstract functions are only declared in base class. Derived classes have to implement those functions in order to inherit that base class. The functions are always defined in derived classes only.

You Have Score    /10