250+ TOP MCQs on Encapsulation and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Encapsulation”.

1. Which among the following best describes encapsulation?
a) It is a way of combining various data members into a single unit
b) It is a way of combining various member functions into a single unit
c) It is a way of combining various data members and member functions into a single unit which can operate on any data
d) It is a way of combining various data members and member functions that operate on those data members into a single unit

Answer: d
Clarification: It is a way of combining both data members and member functions, which operate on those data members, into a single unit. We call it a class in OOP generally. This feature have helped us modify the structures used in C language to be upgraded into class in C++ and other languages.

2. If data members are private, what can we do to access them from the class object?
a) Create public member functions to access those data members
b) Create private member functions to access those data members
c) Create protected member functions to access those data members
d) Private data members can never be accessed from outside the class

Answer: a
Clarification: We can define public member functions to access those private data members and get their value for use or alteration. They can’t be accessed directly but is possible to be access using member functions. This is done to ensure that the private data doesn’t get modified accidentally.

3. While using encapsulation, which among the following is possible?
a) Code modification can be additional overhead
b) Data member’s data type can be changed without changing any other code
c) Data member’s type can’t be changed, or whole code have to be changed
d) Member functions can be used to change the data type of data members

Answer: b
Clarification: Data member’s data type can be changed without changing any further code. All the members using that data can continue in the same way without any modification. Member functions can never change the data type of same class data members.

4. Which feature can be implemented using encapsulation?
a) Inheritance
b) Abstraction
c) Polymorphism
d) Overloading

Answer: b
Clarification: Data abstraction can be achieved by using encapsulation. We can hide the operation and structure of actual program from the user and can show only required information by the user.

5. Find which of the following uses encapsulation?
a) void main(){ int a; void fun( int a=10; cout<<a); fun(); }
b) class student{ int a; public: int b;};
c) class student{int a; public: void disp(){ cout<<a;} };
d) struct topper{ char name[10]; public : int marks; }

Answer: c
Clarification: It is the class which uses both the data members and member functions being declared inside a single unit. Only data members can be there in structures also. And the encapsulation can only be illustrated if some data/operations are associated within class.

6. Encapsulation helps in writing ___________ classes in java.
a) Mutable
b) Abstract
c) Wrapper
d) Immutable

Answer: d
Clarification: Immutable classes are used for caching purpose generally. And it can be created by making the class as final and making all its members private.

7. Which among the following should be encapsulated?
a) The data which is prone to change is near future
b) The data prone to change in long terms
c) The data which is intended to be changed
d) The data which belongs to some other class

Answer: a
Clarification: The data prone to change in near future is usually encapsulated so that it doesn’t get changed accidentally. We encapsulate the data to hide the critical working of program from outside world.

8. How can Encapsulation be achieved?
a) Using Access Specifiers
b) Using only private members
c) Using inheritance
d) Using Abstraction

Answer: a
Clarification: Using access specifiers we can achieve encapsulation. Using this we can in turn implement data abstraction. It’s not necessary that we only use private access.

9. Which among the following violates the principle of encapsulation almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables

Answer: b
Clarification: Global variables almost always violates the principles of encapsulation. Encapsulation says the data should be accessed only by required set of elements. But global variable is accessible everywhere, also it is most prone to changes. It doesn’t hide the internal working of program.

10. Which among the following would destroy the encapsulation mechanism if it was allowed in programming?
a) Using access declaration for private members of base class
b) Using access declaration for public members of base class
c) Using access declaration for local variable of main() function
d) Using access declaration for global variables

Answer: a
Clarification: If using access declaration for private members of base class was allowed in programming, it would have destroyed whole concept of encapsulation. As if it was possible, any class which gets inherited privately, would have been able to inherit the private members of base class, and hence could access each and every member of base class.

11. Which among the following can be a concept against encapsulation rules?
a) Using function pointers
b) Using char* string pointer to be passed to non-member function
c) Using object array
d) Using any kind of pointer/array address in passing to another function

Answer: d
Clarification: If we use any kind of array or pointer as data member which should not be changed, but in some case its address is passed to some other function or similar variable. There are chances to modify its whole data easily. Hence Against encapsulation.

12. Consider the following code and select the correct option.

class student
{  
     int marks;
     public : int* fun()
     { 
          return &marks; 
     }
};
main()
{
    student s;
    int *ptr=c.fun();
    return 0;
}

a) This code is good to go
b) This code may result in undesirable conditions
c) This code will generate error
d) This code violates encapsulation

Answer: d
Clarification: This code violates the encapsulation. By this code we can get the address of the private member of the class, hence we can change the value of private member, which is against the rules.

13. Consider the code and select the wrong choice.

class hero
{ 
     char name[10];
     public : void disp()
     { 
          cout<<name;
     }
};

a) This maintains encapsulation
b) This code doesn’t maintain encapsulation
c) This code is vulnerable
d) This code gives error

Answer: a
Clarification: This code maintains encapsulation. Here the private member is kept private. Outside code can’t access the private members of class. Only objects of this class will be able to access the public member function at maximum.

14. Encapsulation is the way to add functions in a user defined structure.
a) True
b) False

Answer: b
Clarification: False, because we can’t call these structures if member functions are involved, it must be called class. Also, it is not just about adding functions, it’s about binding data and functions together.

15. Using encapsulation data security is ___________
a) Not ensured
b) Ensured to some extent
c) Purely ensured
d) Very low

Answer: b
Clarification: The encapsulation can only ensure data security to some extent. If pointer and addresses are misused, it may violate encapsulation. Use of global variables also makes the program vulnerable, hence we can’t say that encapsulation gives pure security.

Leave a Reply

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