• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ Virtual Functions

Question 1

Which of the following is true about virtual functions in C++.
  • Virtual functions are functions that can be overridden in derived class with the same signature.
  • Virtual functions enable run-time polymorphism in a inheritance hierarchy.
  • If a function is \'virtual\' in the base class, the most-derived class\'s implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. In non-virtual functions, the functions are called according to the type of reference or pointer.
  • All of the above

Question 2

Predict output of the following program C
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() { cout<<\" In Base \\n\"; }
};

class Derived: public Base
{
public:
    void show() { cout<<\"In Derived \\n\"; }
};

int main(void)
{
    Base *bp = new Derived;
    bp->show();

    Base &br = *bp;
    br.show();

    return 0;
}
  • In Base 
    In Base 
  • In Base 
    In Derived
  • In Derived
    In Derived
  • In Derived
    In Base 

Question 3

Output of following program C
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() { cout<<\" In Base \\n\"; }
};

class Derived: public Base
{
public:
    void show() { cout<<\"In Derived \\n\"; }
};

int main(void)
{
    Base *bp, b;
    Derived d;
    bp = &d;
    bp->show();
    bp = &b;
    bp->show();
    return 0;
}
  • In Base 
    In Base 
    
  • In Base 
    In Derived
    
  • In Derived
    In Derived
    
  • In Derived
    In Base 
    

Question 4

Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created.
  • Both 1 and 2
  • Only 1
  • Only 2
  • Neither 1 nor 2

Question 5

C
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() = 0;
};

int main(void)
{
    Base b;
    Base *bp;
    return 0;
}
  • There are compiler errors in lines "Base b;" and "Base bp;"
  • There is compiler error in line "Base b;"
  • There is compiler error in line "Base bp;"
  • No compiler Error

Question 6

Predict the output of following program. CPP
#include<iostream>
using namespace std;
class Base
{
public:
    virtual void show() = 0;
};

class Derived : public Base { };

int main(void)
{
    Derived q;
    return 0;
}
  • Compiler Error: there cannot be an empty derived class
  • Compiler Error: Derived is abstract
  • No compiler Error

Question 7

C
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() = 0;
};

class Derived: public Base
{
public:
    void show() { cout<<\"In Derived \\n\"; }
};

int main(void)
{
    Derived d;
    Base &br = d;
    br.show();
    return 0;
}
  • Compiler Error in line "Base &br = d;"
  • Empty Output
  • In Derived

Question 8

Can a constructor be virtual? Will the following program compile? c
#include <iostream>
using namespace std;
class Base {
public:
  virtual Base() {}   
};
int main() {
   return 0;
}
  • Yes
  • No

Question 9

Can a destructor be virtual? Will the following program compile? c
#include <iostream>
using namespace std;
class Base {
public:
  virtual ~Base() {}   
};
int main() {
   return 0;
}
  • Yes
  • No

Question 10

C
#include<iostream>
using namespace std;
class Base  {
public:
    Base()    { cout<<\"Constructor: Base\"<<endl; }
    virtual ~Base()   { cout<<\"Destructor : Base\"<<endl; }
};
class Derived: public Base {
public:
    Derived()   { cout<<\"Constructor: Derived\"<<endl; }
    ~Derived()  { cout<<\"Destructor : Derived\"<<endl; }
};
int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}
  • Constructor: Base
    Constructor: Derived
    Destructor : Derived
    Destructor : Base
  • Constructor: Base
    Constructor: Derived
    Destructor : Base
  • Constructor: Base
    Constructor: Derived
    Destructor : Derived
  • Constructor: Derived
    Destructor : Derived

There are 14 questions to complete.

Last Updated :
Take a part in the ongoing discussion