Object Oriented Programming using C++ Questions and Answers – Encapsulation

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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.
advertisement
advertisement

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

Answer: b
Explanation: 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; }
View Answer

Answer: c
Explanation: 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.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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.
advertisement

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.