Polymorphism in C++

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

Output of following program C
#include
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 3

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 4

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

Question 5

Which of the following statements are true regarding C++?

(a) Overloading gives the capability to an existing operator to operate on other data types.

(b) Inheritance in object oriented programming provides support to reusability.

(c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.

(d) Overloading is a type of polymorphism.

Choose the correct option from those given below:

  • A

    (a) and (b) only     

  • B

    (a), (b) and (c) only

  • C

    (a), (b) and (d) only     

  • D

    (b), (c) and (d) only

Question 6

What is polymorphism in OOP?

  • A

    The ability of a class to inherit from multiple classes

  • B

    The ability to redefine methods in derived classes

  • C

    The ability to use the same interface for different data types

  • D

    The ability to hide the implementation details of a class

Question 7

How run-time polymorphisms are implemented in C++?

  • A

    Using Inheritance

  • B

    Using Friend functions

  • C

    Using Templates

  • D

    Using Virtual Functions

Question 8

Which of the following is a static polymorphism mechanism?

  • A

    Function overloading

  • B

    Operator overloading

  • C

    Templates

  • D

    All of the mentioned

Tags:

There are 8 questions to complete.

Take a part in the ongoing discussion