Test Series - oops

Test Number 8/78

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which feature can be implemented using encapsulation?
A. Inheritance
B. Abstraction
C. Polymorphism
D. Overloading
Solution: 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.
Q: Find which of the following uses encapsulation?
A. void main(){ int a; void fun( int a=10; cout<
B. class student{ int a; public: int b;};
C. class student{int a; public: void disp(){ cout<
D. struct topper{ char name[10]; public : int marks; }
Solution: 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.
Q: Encapsulation helps in writing ___________ classes in java.
A. Mutable
B. Abstract
C. Wrapper
D. Immutable
Solution: 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.
Q: 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
Solution: 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.
Q: How can Encapsulation be achieved?
A. Using Access Specifiers
B. Using only private members
C. Using inheritance
D. Using Abstraction
Solution: 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.
Q: Which among the following violates the principle of encapsulation almost always?
A. Local variables
B. Global variables
C. Public variables
D. Array variables
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10