Question 51
MediumHow does encapsulation make a class easier to change later?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
How does encapsulation make a class easier to change later?
It removes the need for testing
The internal representation can change without affecting code that uses the public methods
It makes all methods static
It allows multiple inheritance
Sign up free to see the answer and a step-by-step explanation.
What is the main purpose of a package (Java) or namespace (C++) regarding encapsulation?
Making classes faster
Creating objects
Handling exceptions
Grouping related classes and controlling visibility at a larger scale
Sign up free to see the answer and a step-by-step explanation.
Which is NOT an example of encapsulation?
An inner helper class marked private
A private field with a validating setter
A subclass overriding a method of its parent
A module exposing only a few public functions
Sign up free to see the answer and a step-by-step explanation.
In C++, members of a struct are by default:
protected
private
static
public
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Secret {
int x = 5;
};
int main() {
Secret obj;
return obj.x;
}
0
Compilation error
Sign up free to see the answer and a step-by-step explanation.
Why do immutable classes (such as Java's String) improve encapsulation?
They are faster to compile
Their state can't change after creation, so they can be shared safely without defensive copies
They can't have methods
They are stored on disk
Sign up free to see the answer and a step-by-step explanation.
A class returns its private internal list directly from a getter. What is the risk?
No risk at all
Callers can modify the list and break the class's invariants
The list becomes read-only
The program won't compile
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Item:
def __init__(self):
self._price = 0
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if value < 0:
raise ValueError
self._price = value
i = Item()
try:
i.price = -10
print("Rs.", i.price)
except ValueError:
print("Invalid")
Invalid
None
Sign up free to see the answer and a step-by-step explanation.
Which statement about encapsulation and abstraction is most accurate?
Encapsulation is only about inheritance
Abstraction decides what to expose (the interface); encapsulation hides how it is done and protects the data
They mean exactly the same thing
Abstraction requires public fields
Sign up free to see the answer and a step-by-step explanation.
In C++, can private members of one object be accessed by another object of the same class?
Yes — access control is per class, not per object
Only through friend functions
No, never
Only if the members are static
Sign up free to see the answer and a step-by-step explanation.
5
Garbage value
Rs. -10
Error