Free Practice Quiz Question List

3 Encapsulation and Information Hiding Online Quiz Questions

Use this free practice quiz with 20 questions to review 3 Encapsulation and Information Hiding, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which description best captures the practice of encapsulation?

  1. A

    Separating every field into a different class

  2. B

    Grouping state and behavior while controlling access to them

  3. C

    Making every member of a class public

  4. D

    Using only immutable objects

02
True or false
1 point

True or false: Information hiding and encapsulation are related, but information hiding specifically focuses on concealing implementation details that clients do not need to know.

  1. A

    True

  2. B

    False

03
Written response
1 point

What access level should a field use when it must be available only within its declaring class or type?

04
Fill in the blank
1 point

A(n) is a condition that should remain true for every valid object.

05
Choose one
1 point

Which design best preserves encapsulation for a temperature value that must not be below -273.15 degrees Celsius?

  1. A

    A private field with a controlled setter that rejects values below -273.15

  2. B

    A public field so every caller can update the temperature directly

  3. C

    A public field together with a comment telling callers to validate values

  4. D

    A private field with no operation for reading or changing the temperature

06
True or false
1 point

True or false: A property with a public getter and private setter allows client code to read a value while preventing arbitrary replacement of that value.

  1. A

    True

  2. B

    False

07
Written response
1 point

In the Thermostat example, what is the maximum allowed target temperature? Enter the value in degrees Celsius.

08
Choose all
1 point

Select all benefits that the material directly associates with encapsulation.

  1. A

    Protecting object state from invalid changes

  2. B

    Hiding complicated implementation details

  3. C

    Allowing any caller to modify fields without restrictions

  4. D

    Reducing dependencies between classes

09
Fill in the blank
1 point

A Playlist can expose operations such as addSong and playNext without revealing whether its songs are stored in a . Concealing that implementation decision illustrates .

10
Choose one
1 point

Where should validation normally occur to ensure that an object's invariant is maintained?

  1. A

    Only in documentation for the class

  2. B

    Only in code that calls the class

  3. C

    At the constructor, setter, factory method, or domain operation that changes the state

  4. D

    Only after an invalid value has already been stored

11
Choose all
1 point

Select all practices that support a clear public interface.

  1. A

    Use operations that express the object's responsibility

  2. B

    Minimize the public surface

  3. C

    Require callers to know which internal algorithm is used

  4. D

    Return suitable abstractions instead of unnecessary concrete implementation details

12
Open ended
1 point

Explain why replacing a public mutable field with private state and controlled operations usually improves both encapsulation and information hiding. Include what happens to validation, object invariants, and client code when the internal representation changes.

13
Choose one
1 point

Which statement correctly describes a property that exposes a FullName value?

  1. A

    A property must always correspond directly to one stored field

  2. B

    A property may compute a value from other object data

  3. C

    A property cannot have different visibility for its accessors

  4. D

    A property is the same as a public mutable field

14
True or false
1 point

True or false: Returning an object's actual internal mutable list is risky because callers can modify it without passing through the object's validation or control logic.

  1. A

    True

  2. B

    False

15
Choose one
1 point

Which design best encapsulates a bank account whose balance must never become negative?

  1. A

    public balance with unrestricted assignment

  2. B

    private balance with validated deposit and withdraw operations

  3. C

    public balance with validation documented for callers

  4. D

    private balance with no operations for changing it

16
Choose one
1 point

A field is used only by methods in its declaring class. Which access level best follows the recommended default for access control?

  1. A

    public

  2. B

    protected

  3. C

    private

  4. D

    package-, module-, or assembly-level access

17
Choose one
1 point

A User object's email should be readable by client code but replaceable only through the class's validation logic. Which property design is most appropriate?

  1. A

    a public field

  2. B

    a public getter and public setter with no validation

  3. C

    a private getter and public setter

  4. D

    a public getter and private setter

18
Choose one
1 point

Where should a class validate a value that must satisfy an invariant whenever it enters or changes the object?

  1. A

    inside the object's constructor or state-changing operation

  2. B

    only in documentation for client programmers

  3. C

    only in the method that later reads the value

  4. D

    in every client that happens to use the object

19
Written response
1 point

Which access modifier makes a member available only within its declaring class or type?

20
Written response
1 point

What is the technical term for a condition that should remain true for every valid object?