C++ OOPs Concepts MCQ

Object-Oriented Programming (OOP) in C++ is a fundamental paradigm that every developer should be familiar with. OOP concepts, such as classes, objects, inheritance, polymorphism, etc., help in writing clean, modular, and reusable code. In this blog post, let’s challenge your understanding of OOP concepts in C++ with a quiz tailored for beginners.

After each question, you’ll find the correct answer and a brief explanation. Ready to test your knowledge?

1. Which of the following is not an OOP principle in C++?

a) Inheritance
b) Polymorphism
c) Compilation
d) Encapsulation

Answer:

c) Compilation

Explanation:

Compilation is the process of converting source code into machine code. It is not a principle of OOP.

2. What is the primary purpose of a constructor in a class?

a) To construct the class
b) To initialize the class’s objects
c) To destroy the class’s objects
d) None of the above

Answer:

b) To initialize the class’s objects

Explanation:

A constructor is a special member function that is invoked when an object of the class is created. Its primary purpose is to initialize the object.

3. How many types of inheritance does C++ support?

a) 1
b) 3
c) 5
d) 7

Answer:

c) 5

Explanation:

C++ supports five types of inheritance – Single, Multiple, Multilevel, Hierarchical, and Hybrid inheritance.

4. Which keyword is used to prevent overriding in derived classes?

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

Answer:

c) final

Explanation:

The final keyword can be used to prevent derived classes from overriding a method.

5. Which of the following is not a type of constructor?

a) Default constructor
b) Copy constructor
c) Multi constructor
d) Parameterized constructor

Answer:

c) Multi constructor

Explanation:

There is no “Multi constructor” in C++.

6. Which of the following concepts hides the internal data from outside access?

a) Abstraction
b) Encapsulation
c) Polymorphism
d) Inheritance

Answer:

b) Encapsulation

Explanation:

Encapsulation is the concept of bundling data (attributes) and methods (functions) into a single unit and restricting access to the data.

7. What is an abstract class?

a) A class that can be instantiated
b) A class with at least one pure virtual function
c) A class without any members
d) A class with multiple inheritance

Answer:

b) A class with at least one pure virtual function

Explanation:

An abstract class cannot be instantiated and is meant to be subclassed. It contains at least one pure virtual function.

8. Which keyword is used to declare a pure virtual function?

a) abstract
b) virtual
c) pure
d) virtual… = 0

Answer:

d) virtual… = 0

Explanation:

A pure virtual function is declared using virtual followed by = 0.

9. Which concept allows C++ to implement the “is-a” relationship?

a) Composition
b) Encapsulation
c) Inheritance
d) Polymorphism

Answer:

c) Inheritance

Explanation:

The “is-a” relationship is represented through inheritance in OOP.

10. What is the base class in C++ from which all other classes implicitly inherit?

a) Object
b) Base
c) Main
d) None of the above

Answer:

d) None of the above

Explanation:

Unlike some other object-oriented languages, C++ doesn’t have a universal base class from which all other classes implicitly inherit.

11. Which function cannot be overloaded in C++?

a) Main function
b) Constructor
c) Destructor
d) Virtual functions

Answer:

c) Destructor

Explanation:

Destructors cannot be overloaded as a class can have only one destructor.

12. What does the :: operator represent in C++?

a) Scope resolution operator
b) Conditional operator
c) Bitwise operator
d) Shift operator

Answer:

a) Scope resolution operator

Explanation:

The :: operator in C++ is known as the scope resolution operator. It’s used to define a member function outside a class or to access a global variable within the scope where a local variable with the same name exists.

13. What is the default access specifier for members of a class?

a) public
b) private
c) protected
d) None of the above

Answer:

b) private

Explanation:

By default, members of a class are private in C++.

14. Which keyword is used for dynamic polymorphism?

a) dynamic
b) polymorph
c) virtual
d) override

Answer:

c) virtual

Explanation:

Dynamic polymorphism is achieved using virtual functions in C++.

15. Which of the following is correct about the destructor?

a) It is used to initialize the object
b) It has a return type
c) It cannot have parameters
d) It is invoked automatically when the object goes out of scope

Answer:

c) It cannot have parameters and d) It is invoked automatically when the object goes out of scope

Explanation:

A destructor is used to release resources before an object is destroyed. It doesn’t have a return type and cannot accept parameters. It’s automatically called when an object goes out of scope.

16. Which of the following cannot be static in a class?

a) Variables
b) Methods
c) Constructors
d) Destructors

Answer:

c) Constructors

Explanation:

Constructors in C++ cannot be declared as static. However, methods, variables, and destructors can be.

17. Which of the following is a mechanism of reusing code in C++?

a) Copy-Pasting
b) Linking
c) Inheritance
d) Instantiation

Answer:

c) Inheritance

Explanation:

Inheritance allows a class (derived class) to inherit properties and behaviors (methods) from another class (base class), thereby promoting code reuse.

18. What is the process by which one class can acquire the properties and behaviors of another class?

a) Polymorphism
b) Abstraction
c) Inheritance
d) Encapsulation

Answer:

c) Inheritance

Explanation:

Inheritance is the process by which a new class (called a derived class) can acquire the properties and behaviors (methods) of an existing class (base class).

19. Which keyword is used to prevent a class from being inherited?

a) private
b) protected
c) sealed
d) final

Answer:

d) final

Explanation:

In C++, when a class is declared as final, it cannot be inherited by other classes.

20. When is the memory for a class’s member variables allocated?

a) At compile time
b) When the object is declared
c) When the object is used
d) When the class is defined

Answer:

b) When the object is declared

Explanation:

Memory for a class’s member variables is allocated when an object of the class is declared.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top