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

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

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.

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

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

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