C++ Virtual Functions

Last Updated :
Discuss
Comments

Question 1

Which of the following is true about virtual functions in C++.
  • A
    Virtual functions are functions that can be overridden in derived class with the same signature.
  • B
    Virtual functions enable run-time polymorphism in a inheritance hierarchy.
  • C
    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.
  • D
    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;
}
  • A
    In Base 
    In Base 
  • B
    In Base 
    In Derived
  • C
    In Derived
    In Derived
  • D
    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;
}
  • A
    In Base 
    In Base 
    
  • B
    In Base 
    In Derived
    
  • C
    In Derived
    In Derived
    
  • D
    In Derived
    In Base 
    

Question 4

In C++ inheritance, which constructor is called first when an object of a derived class is created?

  • A

    Derived class constructor

  • B

    Base class constructor

  • C

    Constructors are called randomly

  • D

    t depends on access specifier

Question 5

C
#include<iostream>
using namespace std;

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

int main(void)
{
    Base b;
    Base *bp;
    return 0;
}
  • A
    There are compiler errors in lines "Base b;" and "Base bp;"
  • B
    There is compiler error in line "Base b;"
  • C
    There is compiler error in line "Base bp;"
  • D
    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;
}
  • A
    Compiler Error: there cannot be an empty derived class
  • B
    Compiler Error: Derived is abstract
  • C
    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;
}
  • A
    Compiler Error in line "Base &br = d;"
  • B
    Empty Output
  • C
    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;
}
  • A
    Yes
  • B
    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;
}
  • A
    Yes
  • B
    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;
}
  • A
    Constructor: Base
    Constructor: Derived
    Destructor : Derived
    Destructor : Base
  • B
    Constructor: Base
    Constructor: Derived
    Destructor : Base
  • C
    Constructor: Base
    Constructor: Derived
    Destructor : Derived
  • D
    Constructor: Derived
    Destructor : Derived

There are 14 questions to complete.

Take a part in the ongoing discussion