Any2Quiz
ICSE / 10 / Computer Applications

Encapsulation & Access Specifiers Quiz - ICSE Class 10 Computer Applications

Free practice quiz: Encapsulation & Access Specifiers (ICSE Class 10 Computer Applications). 10 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 10 questions free

Sample questions

  1. Which of the following best describes encapsulation in object-oriented programming?

    1. The process of inheriting properties from a parent class.
    2. Bundling data (attributes) and methods (functions) that operate on that data within a single unit (class) and restricting direct access to some of the object's components.
    3. The ability of an object to take on many forms.
    4. The mechanism of sending messages between objects.
    Show answer

    Answer: B. Bundling data (attributes) and methods (functions) that operate on that data within a single unit (class) and restricting direct access to some of the object's components.

    Encapsulation is a fundamental OOP concept that involves bundling data members and member functions within a class. It also involves data hiding, where the data members are kept private and can only be accessed and modified through public methods (getters and setters), thus protecting the internal state of an object.

  2. Consider a class `MyClass` with a private member variable `x`. Which of the following methods would be the correct way to access and modify `x` from outside the class?

    1. Directly accessing `myObject.x`.
    2. Using a public getter method like `getX()` and a public setter method like `setX(value)`.
    3. Using a protected method `accessX()` defined in a subclass.
    4. Using a static method `getPrivateX()` that accesses `x`.
    Show answer

    Answer: B. Using a public getter method like `getX()` and a public setter method like `setX(value)`.

    Private members can only be accessed or modified from within the same class. To allow controlled access from outside, public getter (to retrieve the value) and setter (to modify the value) methods are used, adhering to the principles of encapsulation.

  3. When would you use the `protected` access specifier in Java?

    1. To make members accessible by any class in the project.
    2. To make members accessible only within the same class.
    3. To make members accessible by classes within the same package and by subclasses in any package.
    4. To make members accessible only by classes within the same package.
    Show answer

    Answer: C. To make members accessible by classes within the same package and by subclasses in any package.

    The `protected` access specifier grants access to members from within the same package, and also from subclasses, regardless of which package they reside in. This is useful for providing access to members that are intended for use by derived classes.

  4. Encapsulation helps in creating loosely coupled systems. What does loose coupling imply?

    1. Components are highly dependent on each other's internal implementation details.
    2. Changes in one component have minimal impact on other components.
    3. All components must be tightly integrated for the system to function.
    4. The system is difficult to maintain and modify.
    Show answer

    Answer: B. Changes in one component have minimal impact on other components.

    Loose coupling is a design principle where components of a system have minimal dependencies on each other. Encapsulation, by hiding internal implementation and providing well-defined interfaces, contributes to loose coupling. This makes the system more flexible, maintainable, and easier to update because changes to one part are less likely to break other parts.

  5. If a class member is declared as `public`, where can it be accessed from?

    1. Only within the same class.
    2. Within the same class and subclasses.
    3. Within the same package only.
    4. From any class in any package.
    Show answer

    Answer: D. From any class in any package.

    The `public` access specifier provides the widest scope of accessibility. Members declared as `public` can be accessed from anywhere – within the same class, from other classes in the same package, from subclasses in different packages, and from classes in entirely different packages.

Take the full quiz

Browse all Computer Applications quizzes