Question 71
HardA C++ class has a public member function returning a non-const reference to a private member. What is the effect?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
A C++ class has a public member function returning a non-const reference to a private member. What is the effect?
It doesn't compile
Nothing; it is still private
The member becomes const
Callers can change the private member directly, defeating encapsulation
Sign up free to see the answer and a step-by-step explanation.
What does the Law of Demeter discourage?
Writing constructors
Long chains like a.getB().getC().doThing() that depend on distant objects' structure
Private fields
Using interfaces
Sign up free to see the answer and a step-by-step explanation.
How does encapsulation help when a class's internal representation changes, for example from a list to a map?
Code using only its public methods doesn't need to change
All callers must be rewritten
It forces inheritance
The change is impossible
Sign up free to see the answer and a step-by-step explanation.
In Python, why are single-underscore attributes still accessible from outside?
They are actually private
Python relies on convention rather than enforcement for privacy
It is a bug
Underscores are ignored
Sign up free to see the answer and a step-by-step explanation.
What is an invariant that a BankAccount class might enforce through encapsulation?
The balance never goes below the overdraft limit
The account number changes daily
Methods are public
The class has many fields
Sign up free to see the answer and a step-by-step explanation.
What does the C++ 'mutable' keyword allow, and when is it acceptable?
Making a member const
Changing access levels
Changing a member inside a const method; acceptable for caches or counters that don't affect the object's logical state
Making a class abstract
Sign up free to see the answer and a step-by-step explanation.
Which is the main purpose of package-private (default) access in Java?
Making members public
Speeding up calls
Allowing all subclasses access
Hiding implementation classes and members from code outside the package
Sign up free to see the answer and a step-by-step explanation.
Why is exposing a 'setId' method on an entity often a bad idea?
It breaks inheritance
Setters are slow
IDs must be strings
An entity's identity should not change after creation
Sign up free to see the answer and a step-by-step explanation.
What does Java's 'final' on a field guarantee in multi-threaded code?
Other threads see the field's final value once the constructor finishes (safe publication)
The field can be changed by any thread
The field is volatile
Nothing
Sign up free to see the answer and a step-by-step explanation.
Which change best encapsulates a 'status' field that can only move from DRAFT to PUBLISHED to ARCHIVED?
Use a string field
Make status public
Replace setStatus() with publish() and archive() methods that check the current state
Add more getters
Sign up free to see the answer and a step-by-step explanation.