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.
This ICSE Class 10 Computer Applications practice quiz has 10 multiple-choice questions on Encapsulation & Access Specifiers, and 10 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 10 questions, freeSample questions
Which of the following best describes encapsulation in object-oriented programming?
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.
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?
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.
When would you use the `protected` access specifier in Java?
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.
Encapsulation helps in creating loosely coupled systems. What does loose coupling imply?
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.
If a class member is declared as `public`, where can it be accessed from?
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.
More questions in this quiz
Answers and explanations are in the full quiz.
- In Java, what is the default access specifier for class members if none is explicitly mentioned?
- What is the primary benefit of using the `private` access specifier?
- Which of the following is NOT a valid access specifier in Java?
- Consider a class `BankAccoun`t with a private variable `balance`. Which of the following correctly implements a setter method for `balance`?
- Encapsulation is often described as 'data hiding'. What is the primary goal of data hiding?
Browse all Computer Applications quizzes