Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

Which statement best describes abstraction in object-oriented design?

  1. A

    Abstraction is the same as exposing every implementation detail.

  2. B

    Abstraction presents important capabilities while omitting unnecessary implementation details.

  3. C

    Abstraction means that all object state must be public.

  4. D

    Abstraction applies only to user-interface design.

02
Choose one
1 point

A bank account must prevent its balance from becoming negative. Which design best supports that requirement?

  1. A

    Keep the balance private and require changes to go through validated operations.

  2. B

    Make the balance public so callers can inspect and update it directly.

  3. C

    Provide a public setBalance operation that accepts any value.

  4. D

    Store the balance in a separate class without controlling access to it.

03
Choose one
1 point

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?

  1. A

    Return the internal mutable list so clients can manage items directly.

  2. B

    Expose a public setter for the entire item collection.

  3. C

    Return a read-only view of the internal collection.

  4. D

    Copy the collection into a public mutable list and document that callers should not change it.

04
Choose all
1 point

Which practices are consistent with maintaining object invariants? Select all that apply.

  1. A

    Complete: it checks every condition necessary for validity.

  2. B

    Optional: it is needed only when input comes from an external user.

  3. C

    Consistent: the same rule applies through every public entry point.

  4. D

    Remote: it is best placed in an unrelated utility class.

  5. E

    Non-bypassing: no alternate public path can change the state without validation.

05
Choose all
1 point

Which elements should a reliable public interface document for client code? Select all that apply.

  1. A

    Preconditions that describe what callers must provide.

  2. B

    Postconditions that describe guarantees after successful use.

  3. C

    Private implementation algorithms that clients must reproduce.

  4. D

    Failure behavior and the errors that may occur.

  5. E

    Ownership and lifetime rules for returned objects.

06
True or false
1 point

True or false: Making a member private is, by itself, a complete substitute for authentication and authorization in a sensitive system.

  1. A

    True

  2. B

    False

07
True or false
1 point

True or false: Trimming surrounding whitespace from an otherwise acceptable name is an example of validation rather than normalization.

  1. A

    True

  2. B

    False

08
Written response
1 point

What design principle prevents clients from depending on implementation details that may change, such as whether a document store uses files or a database?

09
Written response
1 point

According to the Temperature example, what is the minimum permitted Celsius value? Enter the value in degrees Celsius.

10
Fill in the blank
1 point

Encapsulation has two related goals: and .

11
Fill in the blank
1 point

A field that callers must not assign directly should normally have visibility. A property that exposes information without permitting ordinary reassignment can be .

12
Open ended
1 point

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.

13
Choose one
1 point

A proposed property setter would perform expensive I/O and publish a message whenever a value changes. Which redesign best follows the interface guidance?

  1. A

    Use a setter because all state changes should look like assignments.

  2. B

    Use an explicitly named method when the operation has significant behavior or side effects.

  3. C

    Expose a public field and document the side effects in a comment.

  4. D

    Make the operation private so clients cannot trigger it.

14
Choose one
1 point

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?

  1. A

    The caller must know whether storage uses a database or local files.

  2. B

    The caller can save and load documents without depending on the storage mechanism.

  3. C

    The caller must access the storage schema directly.

  4. D

    The caller must call several undocumented methods in a particular order.

15
True or false
1 point

True or false: Making a sensitive field private is sufficient by itself to secure a system against unauthorized access.

  1. A

    True

  2. B

    False

16
Written response
1 point

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?

17
Choose one
1 point

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?

  1. A

    It guarantees that all behavior is automatically secure.

  2. B

    It ensures that the class has no implementation details.

  3. C

    It lets callers change state only through operations that can preserve the class rules.

  4. D

    It makes constructors unnecessary because callers can initialize every field directly.

18
Written response
1 point

A Name property trims surrounding whitespace from an otherwise acceptable name before storing it. What design term describes this transformation?

19
Choose one
1 point

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#?

  1. A

    Expose a property with a public getter and a setter accessible only to the class.

  2. B

    Expose a public mutable field so callers can read but should choose not to write it.

  3. C

    Make the value write-only so callers cannot observe it.

  4. D

    Return the internal storage object and document that callers must not modify it.

20
Choose one
1 point

A ShoppingCart must let clients inspect its items without letting them bypass quantity rules. What should getItems() return?

  1. A

    Return the internal list because the caller needs maximum flexibility.

  2. B

    Return a read-only view or defensive copy of the items.

  3. C

    Make add(product, quantity) public but also expose a public mutable list.

  4. D

    Return a setter for the entire collection and validate only during construction.