Which statement best describes abstraction in object-oriented design?
3 Encapsulation and Abstraction Online Quiz Questions
Use this free practice quiz with 20 questions to review 3 Encapsulation and Abstraction, test your knowledge, and prepare for your next test or exam.
A bank account must prevent its balance from becoming negative. Which design best supports that requirement?
- A
Keep the balance private and require changes to go through validated operations.
- B
Make the balance public so callers can inspect and update it directly.
- C
Provide a public setBalance operation that accepts any value.
- D
Store the balance in a separate class without controlling access to it.
A shopping cart must let clients inspect its items without allowing them to add or remove items outside the cart's validation rules. What should the public interface provide?
- A
Return the internal mutable list so clients can manage items directly.
- B
Expose a public setter for the entire item collection.
- C
Return a read-only view of the internal collection.
- D
Copy the collection into a public mutable list and document that callers should not change it.
Which practices are consistent with maintaining object invariants? Select all that apply.
- A
Complete: it checks every condition necessary for validity.
- B
Optional: it is needed only when input comes from an external user.
- C
Consistent: the same rule applies through every public entry point.
- D
Remote: it is best placed in an unrelated utility class.
- E
Non-bypassing: no alternate public path can change the state without validation.
Which elements should a reliable public interface document for client code? Select all that apply.
- A
Preconditions that describe what callers must provide.
- B
Postconditions that describe guarantees after successful use.
- C
Private implementation algorithms that clients must reproduce.
- D
Failure behavior and the errors that may occur.
- E
Ownership and lifetime rules for returned objects.
True or false: Making a member private is, by itself, a complete substitute for authentication and authorization in a sensitive system.
- A
True
- B
False
True or false: Trimming surrounding whitespace from an otherwise acceptable name is an example of validation rather than normalization.
- A
True
- B
False
What design principle prevents clients from depending on implementation details that may change, such as whether a document store uses files or a database?
According to the Temperature example, what is the minimum permitted Celsius value? Enter the value in degrees Celsius.
Encapsulation has two related goals: and .
A field that callers must not assign directly should normally have visibility. A property that exposes information without permitting ordinary reassignment can be .
Explain what it means for an object to be valid by construction and why this design is useful. Include one example of an invariant that could be enforced during construction.
A proposed property setter would perform expensive I/O and publish a message whenever a value changes. Which redesign best follows the interface guidance?
- A
Use a setter because all state changes should look like assignments.
- B
Use an explicitly named method when the operation has significant behavior or side effects.
- C
Expose a public field and document the side effects in a comment.
- D
Make the operation private so clients cannot trigger it.
A FileStore interface offers save(document) and load(documentId), while its implementation may use local files, a database, or cloud storage. Which statement best demonstrates both abstraction and information hiding?
- A
The caller must know whether storage uses a database or local files.
- B
The caller can save and load documents without depending on the storage mechanism.
- C
The caller must access the storage schema directly.
- D
The caller must call several undocumented methods in a particular order.
True or false: Making a sensitive field private is sufficient by itself to secure a system against unauthorized access.
- A
True
- B
False
What design term names a condition that should remain true for every valid object state, such as a date range whose end is not earlier than its start?
A BankAccount exposes deposit(amount) and withdraw(amount) but does not expose its balance as a public mutable field. What is the main design benefit of this choice?
- A
It guarantees that all behavior is automatically secure.
- B
It ensures that the class has no implementation details.
- C
It lets callers change state only through operations that can preserve the class rules.
- D
It makes constructors unnecessary because callers can initialize every field directly.
A Name property trims surrounding whitespace from an otherwise acceptable name before storing it. What design term describes this transformation?
A class must let ordinary callers read Balance while allowing only the class itself to update it. Which design best meets that requirement in C#?
- A
Expose a property with a public getter and a setter accessible only to the class.
- B
Expose a public mutable field so callers can read but should choose not to write it.
- C
Make the value write-only so callers cannot observe it.
- D
Return the internal storage object and document that callers must not modify it.
A ShoppingCart must let clients inspect its items without letting them bypass quantity rules. What should getItems() return?
- A
Return the internal list because the caller needs maximum flexibility.
- B
Return a read-only view or defensive copy of the items.
- C
Make add(product, quantity) public but also expose a public mutable list.
- D
Return a setter for the entire collection and validate only during construction.