1Z0-811 Object-Oriented Programming Practice Question
A company's HR system uses an Employee class with sensitive salary information. The team wants to allow other classes to read the salary but not modify it. Which approach best preserves encapsulation?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Provide a public getSalary() getter method and keep salary private
Encapsulation is achieved by keeping fields private and providing public getters. A getter allows read-only access without exposing the internal representation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Declare the salary field as public
Why it's wrong here
Making the field public violates encapsulation; any class can modify it.
- ✓
Provide a public getSalary() getter method and keep salary private
Why this is correct
Correct. Private field with public getter provides controlled read access.
- ✗
Create a private method that returns salary only when called from within the same package
Why it's wrong here
Private methods are not accessible from outside the class, so others cannot read salary.
- ✗
Provide a public setSalary() setter method and keep salary private
Why it's wrong here
A setter allows modification, which is not desired.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.