Select chapter



constructors (2.papers)
inheritance (1.papers)
overriding (1.papers)
basics (4.papers)
pointers (2.papers)
Hide

Sponsored Links


  

c++ virtual functions interview questions and answers

1
What is an abstract class?
  1. A class with abstract key word is called as abstract class.
  2. A class with at least one pure virtual function in it, is called as abstract class?
  3. A class with at least one pure virtual function in it, and with a abstract keyword at class level declaration is called as abstract class?
  4. none
Show Answer
Share Facebook gmail Twitter
2
Which of the following statements are false regarding virtual functions?
  1. A virtual function can be a friend of another class
  2. they cannot be static members.
  3. We cannot have virtual constructors.
  4. Virtual functions need not be member of any class
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
What is the output of below C++ program?
class Base{
    public:
       virtual void disp()= 0;
};

int main()
{
    cout << "Hello world";
    Base b;
}
  1. Hello world
  2. compile time error in Base b; you can't create object for an abstract class.
  3. compile time error in virtual void disp()=0;
  4. none
Show Answer
Share Facebook gmail Twitter
5
Which are faster a virtual function or non-virtual? Assuming that both has same code.
  1. it depends on cpu
  2. virtual functions are faster as it uses virtual tables internally.
  3. non-virtual functions are faster as there is no extra internal virtual table look up, when that function is called.
  4. we can't predict
Show Answer
Share Facebook gmail Twitter
6
What is the use of having virtual functions then?
  1. You can achieve dynamic polymorphism with it.
  2. you can achieve static polymorphism with it.
  3. it is of no use, more over it adds extra over head of virtual tables for each class.
  4. none
Show Answer
Share Facebook gmail Twitter
7
How the virtual concept works in c++?
  1. If a class has virtual functions, then a virtual table will be created for that class, and pointer to that virtual table will be put in all the objects of that class type.
  2. Derived classes will inherit the virtual tables of base class.
  3. If you override virtual functions in derived class, then the pointer to that virtual function will be added newly in the virtual table of derived class, which overrides the same field in virtual table of base class.
  4. all are true
Show Answer
Share Facebook gmail Twitter
8
What does virtual table contain?
  1. it contains pointer base class virtual table
  2. it contains pointers to all virtual function implementations of that class.
  3. it contains both 1 & 2.
  4. none
Show Answer
Share Facebook gmail Twitter
9
When should I declare my destructor as virtual?
  1. It is a common standard to always have a virtual destructor in your class.
  2. It is up to you.
  3. If your class contains at least one virtual function, then make sure that you declare virtual destructor as well.
  4. none
Show Answer
Share Facebook gmail Twitter
10
Can I call a virtual function from constructor of the class?
  1. why not
  2. no, compile time error
  3. it is not recommended. Because virtual table will be constructed completely only after constructor execution. So if you call a virtual function, it may execute a wrong or undesired function.
  4. yes, it won't create any problem.
Show Answer
Share Facebook gmail Twitter
11
Which of the below statements are false about a static function.
  1. a static function can be virtual
  2. they  can't be declared as const or volatile
  3. you can't have same function version static as well as non-static
  4. all are false
Show Answer
Share Facebook gmail Twitter
12
What is the use of Virtual functions in C++?
  1. If derived class redefines the method of a base class, and if you want to access derived class functionality by using base class pointer then you have to make the function in base class as virtual function.
  2. If derived class redefines the method of a base class, and if you want to access base  class functionality by using base class pointer then you have to make the function in base class as virtual function.
  3. If derived class redefines the method of a base class, and if you want to access derived class functionality by using base class pointer then you have to make the function in derived class as virtual function.
  4. If derived class redefines the method of a base class, and if you want to access derived class functionality by using base class pointer then you don't have to make the function in base class as virtual function.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
16
What is a pure virtual function?
  1. A function which has only signature and no method body.
  2. A function with virtual keyword in its definition.
  3. a function with virtual keyword, and no body, and assigned with 0.
  4. a function with out any body and assigned with 0.
Show Answer
Share Facebook gmail Twitter
17
What is the use of pure virtual functions in c++?
  1. If you want all the derived classes to implement a method, then make it as pure virtual function in your class.
  2. A pure virtual function is same as normal virtual function, other wise there is no use case.
  3. option 1 is wrong, because derived classes need not implement pure virtual function of base class.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :