C++ Polymorphism MCQ

Polymorphism is a cornerstone of Object-Oriented Programming (OOP) in C++. It allows objects to be treated as instances of their parent class rather than their actual type. Ready to dive deep into the realm of C++ polymorphism? Let's test your knowledge with this beginner-friendly quiz!

1. What does the term 'Polymorphism' mean in C++?

a) The ability to have many classes
b) The ability to have many methods
c) The ability to take on many forms
d) The ability to have many variables

Answer:

c) The ability to take on many forms

Explanation:

Polymorphism comes from the Greek words 'poly' meaning many and 'morph' meaning forms. It refers to the ability of one function or method to work in multiple ways depending on its input or on its associated objects.

2. Which of the following best describes runtime polymorphism?

a) Function overloading
b) Operator overloading
c) Templates
d) Virtual functions

Answer:

d) Virtual functions

Explanation:

Runtime polymorphism is achieved through the use of virtual functions, where the function to be executed is determined at runtime based on the object's actual type.

3. What keyword is used to declare a function as polymorphic in the base class?

a) polymorphic
b) override
c) virtual
d) abstract

Answer:

c) virtual

Explanation:

The virtual keyword is used to declare a function as polymorphic in the base class. This allows derived classes to provide a specific implementation of the function.

4. How is compile-time polymorphism achieved in C++?

a) Function overriding
b) Function overloading and operator overloading
c) Using virtual functions
d) Using abstract classes

Answer:

b) Function overloading and operator overloading

Explanation:

Compile-time polymorphism, also known as static polymorphism, is achieved through function overloading and operator overloading.

5. In the context of polymorphism, what does "overriding" mean?

a) Using the same function name in different classes
b) Changing the return type of a function in a derived class
c) Providing a new implementation of a base class function in a derived class
d) Calling a function multiple times with different arguments

Answer:

c) Providing a new implementation of a base class function in a derived class

Explanation:

Overriding refers to the ability of the derived class to provide a specific implementation of a function that is already provided by its base class.

6. Which of the following is NOT a type of polymorphism?

a) Runtime Polymorphism
b) Compile-time Polymorphism
c) Overloading Polymorphism
d) External Polymorphism

Answer:

d) External Polymorphism

Explanation:

There's no concept called "External Polymorphism" in C++.

7. What is the primary requirement for function overriding?

a) Both the base class and derived class functions must have different names
b) The derived class function must be virtual
c) The base class function must be virtual
d) Both functions must be static

Answer:

c) The base class function must be virtual

Explanation:

For function overriding, the function in the base class should be declared as virtual, which allows the derived class to override it.

8. How can you prevent a class from being subclassed further?

a) By making the class virtual
b) By making the class sealed
c) By using the final keyword
d) By using the end keyword

Answer:

c) By using the final keyword

Explanation:

In C++, you can prevent a class from being subclassed by using the final keyword.

9. If a base class destructor is not virtual, what can happen?

a) The derived class destructor will not be called
b) The base class destructor will not be called
c) It will cause a compile-time error
d) The program will crash

Answer:

a) The derived class destructor will not be called

Explanation:

If the base class destructor is not virtual and we delete an object of a derived class using a pointer of the base class, the derived class destructor will not be called. This may lead to resource leaks.

10. If a virtual function is defined in the base class, it...

a) Must be overridden in the derived class
b) Cannot be accessed by objects of the derived class
c) Can optionally be overridden in the derived class
d) Will cause an error in the derived class

Answer:

c) Can optionally be overridden in the derived class

Explanation:

If a virtual function is defined in the base class, the derived class can optionally provide its own implementation or override the base class function. If not overridden, the base class's function is used.

Great job on tackling the C++ Polymorphism quiz! Polymorphism is a powerful concept that enables flexibility and promotes code reusability. Whether you mastered the quiz or encountered challenges, always remember that continuous learning and practice make perfect. Dive deeper into polymorphism and other OOP concepts, and keep honing your coding skills!



Comments