Polymorphism in C++ MCQs Exercise I


Ques 1 Polymorphism


What is polymorphism in C++?
  a) The ability of a class to have multiple constructors
  b) The ability of a class to have multiple member functions with the same name but different parameters
  c) The ability of a class to inherit from multiple base classes
  d) The ability of a class to hide its data members


b is the correct option





Ques 2 Polymorphism


Which type of polymorphism is achieved through function overloading in C++?
  a) Compile-time polymorphism
  b) Runtime polymorphism
  c) Ad-hoc polymorphism
  d) Static polymorphism


a is the correct option





Ques 3 Polymorphism


What is dynamic polymorphism in C++?
  a) It is the polymorphism achieved through operator overloading.
  b) It is the polymorphism achieved through function templates.
  c) It is the polymorphism that occurs at runtime and is achieved through virtual functions and inheritance
  d) It is the polymorphism that occurs at compile-time and is achieved through function overloading.


c is the correct option





Ques 4 Polymorphism


What will be the output of the following C++ code?

#include<iostream>
class Animal {
public:
    virtual void makeSound() const {
        std::cout << "Generic animal sound" << std::endl;
    }
};
class Dog : public Animal {
public:
    void makeSound() const override {
        std::cout << "Woof Woof!" << std::endl;
    }
};
int main() {
    Animal* animal = new Dog();
    animal->makeSound();

    delete animal;
    return 0;
}

  a) Generic animal sound
  b) Woof Woof!
  c) Compilation Error
  d) Runtime Error


a is the correct option





Ques 5 Polymorphism


What will be the output of the following C++ code?

#include<iostream>
class Shape {
public:
    virtual void draw() const {
        std::cout << "Drawing a Shape" << std::endl;
    }
};
class Circle : public Shape {
public:
    void draw() const override {
        std::cout << "Drawing a Circle" << std::endl;
    }
};
int main() {
    Shape* shape1 = new Circle();
    shape1->draw();
    Circle circle;
    Shape& shape2 = circle;
    shape2.draw();
    return 0;
}

  a) Drawing a Shape Drawing a Shape
  b) Drawing a Circle Drawing a Circle
  c) Drawing a Circle Drawing a Shape
  d) Compilation Error


c is the correct option





Ques 6 Polymorphism


What will be the output of the following C++ code?

#include<iostream>
class A {
public:
    virtual void show() {
        std::cout << "A" << std::endl;
    }
};
class B : public A {
public:
    void show() override {
        std::cout << "B" << std::endl;
    }
};
int main() {
    A* ptrA = new A;
    A* ptrB = new B;
    ptrA->show();
    ptrB->show();
    delete ptrA;
    delete ptrB;
    return 0;
}

  a) A B
  b) B B
  c) A A
  d) B A


a is the correct option