1.
A virtual function that has no definition within the base class is called____________.

(a) Pure virtual function
(b) Pure static function
(c) Pure Const function
(d) Friend function

Answer
(a) If there is a condition that derived class must override the particular function or complete definition of a function is not defined in the base class then we will declare function as a virtual function in the base class.

2.
If a class contains pure virtual function, then it is termed as____________________ .

(a) Virtual class
(b) Sealed class
(c) Pure Local class
(d) Abstract Class

Answer
(d) Abstract classes are used to provide an Interface for its sub classes and it must have pure virtual function.

3.
When a virtual function is redefined by the derived class, it is called___________.

(a) Overloading
(b) Overriding
(c) Rewriting
(d) All of these

Answer
(b) When a virtual function is redefined by the derived class, it is called as overriding. In overriding the parameters of method is not change.

4.
Which of the followings are true about Virtual functions?

(a) They must be non-static member function of the class
(b) They cannot be friends
(c) Constructor Functions cannot be virtual
(d) All of these

Answer
(d)
The important points about Virtual functions are as follows:
• They must be non-static member function of the class.
• They cannot be friends.
• Constructor cannot be virtual but destructor can be virtual.
• It is used for late binding or runtime binding.
• The address of the virtual Function is placed in the VTABLE.

5.
Find the wrong statement/s about Abstract Class.

(a) We can’t create its objects.
(b) We can’t create pointers to an abstract class.
(c) It contains at least one pure virtual function.
(d) We can create references to an abstract class.

Answer
(b) We cannot create objects of the abstract class, but pointers and refrences of Abstract class type can be created.

6.
Syntax for Pure Virtual Function is ______________ .

(a) virtual void show()==0
(b) void virtual show()==0
(c) virtual void show()=0
(d) void virtual show()=0

Answer
(c) A virtual function that has no definition within the base class is called as pure virtual function.

7.
Run time polymorphism can be achieved with______ .

(a) Virtual Base class
(b) Container class
(c) Virtual function
(d) Both a and c

Answer
(c) Run time polymorphism can be achieved with virtual function. To create virtual function, precede the function’s declaration in the base class with the keyword virtual.

8.
If abstract class is inherited by derived class, then_______________ .

(a) Derived class should provide definition for all the pure virtual functions
(b) Derived class also become abstract if fails to implement pure virtual functions
(c) Objects of derived class can’t be created if it fails to implement pure virtual functions
(d) All of these

Answer
(d) If abstract class is inherited by derived class, then Derived class should provide definition for all the pure virtual functions otherwise derived class also become abstract if fails to implement pure virtual functions. If derived class is also become abstract then you cannot create object of derived class.

9.
What is the error in the following code?

class t
{
virtual void print();
}

(a) No error.
(b) Function print() should be declared as static.
(c) Function print() should be defined.
(d) Class t should contain data members.

Answer
(a) no explanation.

10.
Pick out the correct option.

(a) We cannot make an instance of an abstract base class
(b) We can make an instance of an abstract base class
(c) Both a & b
(d) None of the mentioned.

Answer
(a) no explanation.
Virtual Function
Tagged on: