Table of Contents
This exam objective tests your knowledge on how to write a class that meets the encapsulation principles and validate your understanding.
I have prepared a set of good mock questions for this objective that might be useful for the readers who are preparing for the OCAJP 8 certification exam.
Encapsulation mock questions
1. Which Java feature uses access modifiers to protect class data (variables)?
A. Abstraction
B. Inheritance
C. Polymorphism
D. Encapsulation
E. Data hiding
2. What is Encapsulation ?
A. Variables are public and public setter/getter methods are provided to change/access their values.
B. Variables are default and protected setter/getter methods are provided to change/access their values.
C. Variables are protected and public setter/getter methods are provided to change/access their values. D. Variables are private and protected setter/getter methods are provided to change/access their values.
E. Variables are private and public setter/getter methods are provided to change/access their values.
3. Which are true about Encapsulation ? (select 2 options)
A. A class can have total control over what data is stored in its fields.
B. Classes can get functionality from other classes.
C. Classes expose only certain fields and methods to other classes for access.
D. Classes contains abstract methods which must be implemented by it’s sub classes.
4. How can you make the below class to exhibit fully encapsulation ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Student { public int id; protected String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } } |
A. No need of changes.
B. Make all properties private.
C. Make all methods protected.
D. Make all properties private and provide setter method for name property.
E. Make all properties private and remove setter method for id property.
5. How can you make the below class to exhibit fully encapsulation ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class Rectangle { public int length; public int width; public int area; public void setLength(int length) { this.length = length; calculateArea(); } public void setWidth(int width) { this.width = width; calculateArea(); } private void calculateArea() { this.area = this.length * this.width; } protected int getArea() { return area; } } |
B. Make all properties private.
C. Make all methods protected.
D. Make all properties private and provide setter method for area property.
E. Make all properties private and getArea method public.
Answers
1. Correct option : D Encapsulation uses access modifiers to protect variables.
2. Correct option : E To make a class as fully encapsulated, make variables as private and provide setters methods to assign or change variable values and provide getters methods to get variable values.
3. Correct options : A, C With Encapsulation your classes can expose only certain fields and methods to other classes for access and class can have total control over what data is stored in its fields.
4. Correct option : D To make the above class as fully encapsulated, make all variables as private and give setter method for name property for assigning data to name property.
5. Correct option : E To make the above class as fully encapsulated, make all variables as private and give public to getArea method.
If you are preparing for the OCAJP certification exam, the following references would be much useful.
• OCAJP 8 Exam Curriculum
• OCAJP Forums
• How to prepare for OCAJP Exam?