Explore fundamental concepts of Object-Oriented Programming (OOP) with these 30 MCQs. Topics include classes and objects, constructors and destructors, access modifiers, and encapsulation in C++. Perfect for mastering core OOP principles.
MCQs on Basic Object-Oriented Programming (OOP) Concepts
Classes and Objects
What is a class in C++?
a) A variable type
b) A blueprint for creating objects
c) A data structure used for functions
d) A type of loop
Which of the following statements is true about objects?
a) Objects are a blueprint of classes
b) Objects are instances of classes
c) Objects do not have attributes
d) Objects only store functions
In OOP, what does an object contain?
a) Only data
b) Only functions
c) Data and functions
d) Only public members
Which keyword is used to define a class in C++?
a) object
b) new
c) class
d) struct
What is the relationship between a class and an object?
a) Classes are instances of objects
b) Objects are instances of classes
c) They are the same
d) Classes contain only functions, while objects contain data
Constructors and Destructors
What is the purpose of a constructor in a class?
a) To delete an object
b) To initialize objects of a class
c) To overload functions
d) To define data types
Which of the following statements is true about destructors?
a) They are used to initialize an object
b) They are called automatically when an object is destroyed
c) They have parameters
d) They cannot be defined by the user
How many constructors can a class have in C++?
a) Only one
b) Only two
c) One or more
d) None
When is a destructor called in C++?
a) When an object is created
b) When an object goes out of scope or is deleted
c) When a function is called
d) When a class is defined
Which of the following correctly defines a destructor in C++?
a) ~ClassName()
b) ClassName::~()
c) delete ClassName()
d) ClassName::~ClassName()
Can constructors be overloaded in C++?
a) Yes, by defining multiple constructors with different parameters
b) No, constructors cannot be overloaded
c) Only if the class has no destructor
d) Only if the class has no private members
Which type of constructor is automatically called when no arguments are provided?
a) Parameterized constructor
b) Copy constructor
c) Default constructor
d) Destructor
Which of the following statements is true about the copy constructor?
a) It initializes an object by copying another object of the same class
b) It destroys an object
c) It is used to overload functions
d) It is automatically called when an object is deleted
What is the role of a parameterized constructor?
a) It initializes members of an object with given arguments
b) It only destroys an object
c) It has no parameters
d) It is used to initialize private members only
What is the syntax for declaring a default constructor in C++?
a) ClassName() {}
b) ~ClassName() {}
c) ClassName(void);
d) ClassName() with default
Access Modifiers (public, private, protected)
What is the default access modifier for class members in C++?
a) Public
b) Private
c) Protected
d) None
Which access modifier makes a class member accessible only within its own class?
a) Public
b) Private
c) Protected
d) Friend
If a member is declared as public, where can it be accessed from?
a) Only within the class
b) Within the class and its derived classes
c) From anywhere in the program
d) Only within the same file
Which access modifier allows derived classes to access members but restricts outside classes?
a) Public
b) Private
c) Protected
d) Friend
What happens if no access modifier is specified for a class member in C++?
a) It defaults to public
b) It defaults to private
c) It defaults to protected
d) It becomes inaccessible
In a class, if you want certain methods accessible only within the same class, which modifier should you use?
a) Public
b) Private
c) Protected
d) Static
How does the protected access modifier affect member accessibility?
a) Accessible only within the class
b) Accessible within the class and derived classes
c) Accessible from anywhere in the program
d) Not accessible in any class
Can a private member be accessed by an instance of the class?
a) Yes, directly
b) No, only through public methods
c) Yes, through inheritance
d) No, it is not accessible at all
Which access modifier would you use to allow member access only within the same package?
a) Public
b) Private
c) Protected
d) Package
Which of the following keywords is used to make a class accessible to other classes in the same package only?
a) private
b) protected
c) package
d) public
Encapsulation in C++
What is encapsulation in C++?
a) A feature that allows inheritance
b) A mechanism of restricting access to certain details of an object
c) A way to write functions
d) A process for handling multiple objects
How is encapsulation achieved in C++?
a) Through pointers
b) Through classes and access modifiers
c) Through arrays
d) Through operator overloading
Why is encapsulation important in OOP?
a) It provides data hiding and security
b) It simplifies code without providing data hiding
c) It only focuses on function overloading
d) It helps perform arithmetic operations
Which of the following best describes encapsulation?
a) Combining data and methods in a class
b) Using only private members
c) Using only public methods
d) Having global variables
Which of the following is a benefit of encapsulation?
a) It enables data abstraction and hides details
b) It increases code redundancy
c) It makes data globally accessible
d) It allows only public access
Answer Table
Qno
Answer
1
b) A blueprint for creating objects
2
b) Objects are instances of classes
3
c) Data and functions
4
c) class
5
b) Objects are instances of classes
6
b) To initialize objects of a class
7
b) They are called automatically when an object is destroyed
8
c) One or more
9
b) When an object goes out of scope or is deleted
10
a) ~ClassName()
11
a) Yes, by defining multiple constructors with different parameters
12
c) Default constructor
13
a) It initializes an object by copying another object of the same class
14
a) It initializes members of an object with given arguments
15
a) ClassName() {}
16
b) Private
17
b) Private
18
c) From anywhere in the program
19
c) Protected
20
b) It defaults to private
21
b) Private
22
b) Accessible within the class and derived classes
23
b) No, only through public methods
24
d) Package
25
c) package
26
b) A mechanism of restricting access to certain details of an object