Java Encapsulation & Access Practice
Private fields, getters/setters, and access modifiers (private, public, protected).
1
Private Field with Getter
Easy
Create a class BankAccount with a private double balance. Add a public getter getBalance(). Read an amount, set balance to it via a constructor or setter, and print getBalance() as an integer.
Input Format
A single integer amount.
Output Format
A single integer: the balance.
Sample Test Cases
|
Sample Input 1
2500 |
Sample Output 1
2500 |
Constraints
0
Explanation
Access the private field only through the getter.
2
Data Hiding
Easy
Create a class Student with private int marks. Provide a public method boolean isPassing() that returns marks >= 40. Read marks and print "Pass" or "Fail" using the method.
Input Format
A single integer marks.
Output Format
"Pass" or "Fail".
Sample Test Cases
|
Sample Input 1
65 |
Sample Output 1
Pass |
|
Sample Input 2
32 |
Sample Output 2
Fail |
Constraints
0
Explanation
Keep the field private; expose behavior publicly.
1
Validation in Setter
Medium
Create a class Age with private int age and a setter setAge(int age) that ignores values below 0 or above 150 (keeps previous). Read an age, set it, and print the stored age. If the value is invalid the stored value stays 0.
Input Format
A single integer age.
Output Format
A single integer: the stored age.
Sample Test Cases
|
Sample Input 1
25 |
Sample Output 1
25 |
|
Sample Input 2
200 |
Sample Output 2
0 |
|
Sample Input 3
-5 |
Sample Output 3
0 |
Constraints
Any integer input.
Explanation
Validate inside the setter.
2
Read-Only Class
Medium
Create a class Temperature with private double celsius and a constructor. Provide a public method toCelsius() (returns stored value) but no way to change it after creation. Read a value, create the object, and print toCelsius() as an integer.
Input Format
A single integer celsius.
Output Format
A single integer.
Sample Test Cases
|
Sample Input 1
37 |
Sample Output 1
37 |
Constraints
-100
Explanation
No setter — only a constructor and getter.
3
Protected Access (Same Package)
Medium
Create a class Base with protected int value. A subclass Derived extends Base and has a method void setValue(int v) that sets value directly. Read an integer, use setValue, and print value through a getter in Derived.
Input Format
A single integer V.
Output Format
A single integer: the value.
Sample Test Cases
|
Sample Input 1
42 |
Sample Output 1
42 |
Constraints
-10^6
Explanation
Protected members are visible in subclasses.
4
Public vs Private Method
Medium
Create a class Printer with a private method void secret() printing "Secret" and a public method void show() that calls secret() and then prints "Public". In main call show() only.
Input Format
No input.
Output Format
Two lines: Secret, then Public.
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Secret Public |
Constraints
—
Explanation
Public methods can call private ones internally.
1
Encapsulated Student Class
Hard
Create a class Student with private name and marks. Provide a constructor, getters getName()/getMarks(), and a method String getGrade() returning "A" (>=75), "B" (>=60), "C" (>=40), "F" otherwise. Read name and marks and print "Name: , Marks: , Grade: ".
Input Format
Two lines: name, then marks.
Output Format
Name: , Marks: , Grade:
Sample Test Cases
|
Sample Input 1
Riya 85 |
Sample Output 1
Name: Riya, Marks: 85, Grade: A |
|
Sample Input 2
Sam 55 |
Sample Output 2
Name: Sam, Marks: 55, Grade: C |
Constraints
1
Explanation
Combine encapsulation with derived behavior.
2
Getters for Array
Hard
Create a class Scores with a private int[] scores and a constructor that takes the array. Provide getScore(int i). Read N and N scores, create the object, then read an index K and print the element at K or "Index out of range".
Input Format
First line: N. Second line: N scores. Third line: K.
Output Format
The element at K, or "Index out of range".
Sample Test Cases
|
Sample Input 1
4 10 20 30 40 2 |
Sample Output 1
30 |
|
Sample Input 2
4 10 20 30 40 9 |
Sample Output 2
Index out of range |
Constraints
1
Explanation
Validate K inside the getter.
3
Immutable Point
Hard
Create an immutable class Point with final int x, final int y and a constructor. Provide getters only. Read two integers and print "Point(x, y)". Since fields are final, no setters exist.
Input Format
A single line with two integers X Y.
Output Format
Point(X, Y)
Sample Test Cases
|
Sample Input 1
3 4 |
Sample Output 1
Point(3, 4) |
Constraints
-10^6
Explanation
final fields + getters only = immutable.
4
Default Access (Package-Private)
Hard
Create a class Helper in the default package with a method String greet() that has no modifier (package-private) and returns "Hello". Call it from main within the same file (same package) and print the result.
Input Format
No input.
Output Format
Hello
Sample Test Cases
|
Sample Input 1
|
Sample Output 1
Hello |
Constraints
—
Explanation
No modifier = package-private access.
Competitive MCQs — Java Encapsulation & Access
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 31
Q1
What does encapsulation primarily achieve?
Correct!
Wrong — correct answer is .
Encapsulation bundles fields privately and exposes controlled access.
Q2
Which access modifier makes a member visible only within the same class?
Correct!
Wrong — correct answer is .
private members are accessible only inside the declaring class.
Q3
Which access modifier allows access anywhere?
Correct!
Wrong — correct answer is .
public members are accessible from any class.
Q4
What is the default (no modifier) access level in Java?
Correct!
Wrong — correct answer is .
No modifier means accessible within the same package.
Q5
What is the output of this code?
java
1
2
3
4
5
6
7
2
3
4
5
6
7
class A {
private int x = 10;
int getX() {
return x;
}
}
System.out.println(new A().getX());Correct!
Wrong — correct answer is .
The getter returns the private field — 10.
Q6
What is the output of this code?
class A {
private int x = 10;
int getX() {
return x;
}
}
System.out.println(new A().getX());
class A {
private int x = 10;
int getX() {
return x;
}
}
System.out.println(new A().getX());
Correct!
Wrong — correct answer is .
The getter returns the private field — 10.
Q7
Which is the conventional naming for a getter of field balance?
Correct!
Wrong — correct answer is .
Getters are named get() by convention.
Q8
What is the output of this code?
java
1
2
3
4
2
3
4
class A {
private int x = 5;
}
System.out.println(new A().x);Correct!
Wrong — correct answer is .
Private fields cannot be accessed from outside the class.
Q9
What is the output of this code?
class A {
private int x = 5;
}
System.out.println(new A().x);
class A {
private int x = 5;
}
System.out.println(new A().x);
Correct!
Wrong — correct answer is .
Private fields cannot be accessed from outside the class.
Q10
Which modifier allows access in subclasses and same package, but not unrelated packages?
Correct!
Wrong — correct answer is .
protected = package access + subclass access.
Q11
Why use getters/setters instead of public fields?
Correct!
Wrong — correct answer is .
Setters can enforce invariants before mutating state.
Q12
Which keyword marks a member as belonging to the class, not instances?
Correct!
Wrong — correct answer is .
static members are shared at the class level.
Q13
What is the output of this code?
class A {
private int x = 10;
public int getX() {
return x;
}
}
System.out.println(new A().getX());
class A {
private int x = 10;
public int getX() {
return x;
}
}
System.out.println(new A().getX());
Correct!
Wrong — correct answer is .
The public getter exposes the private field value 10.
Q14
Which modifier allows access within the same package and subclasses?
Correct!
Wrong — correct answer is .
protected grants package access plus subclass access.
Q15
What is the default (package) access level?
Correct!
Wrong — correct answer is .
No modifier means package-private.
Q16
Why use getters and setters?
Correct!
Wrong — correct answer is .
Accessors enable validation and internal changes without breaking callers.
Q17
What is the output of this code?
class A {
private int age = 10;
public void setAge(int a) {
if (a > 0) age = a;
}
public int getAge() {
return age;
}
}
A a = new A();
a.setAge(-5);
System.out.println(a.getAge());
class A {
private int age = 10;
public void setAge(int a) {
if (a > 0) age = a;
}
public int getAge() {
return age;
}
}
A a = new A();
a.setAge(-5);
System.out.println(a.getAge());
Correct!
Wrong — correct answer is .
The setter rejects -5, so age remains 10.
Q18
What is the conventional getter for a field named score?
Correct!
Wrong — correct answer is .
Getters are named get followed by the field in PascalCase.
Q19
What is the conventional setter for field name?
Correct!
Wrong — correct answer is .
Setters follow set + FieldName and usually return void.
Q20
Which access modifier offers the least visibility?
Correct!
Wrong — correct answer is .
private restricts access to the enclosing class only.
Q21
What is data hiding?
Correct!
Wrong — correct answer is .
Data hiding is encapsulation's core benefit.
Q22
What is the output of this code?
class A {
protected int x = 7;
}
class B extends A {
int getX() {
return x;
}
}
System.out.println(new B().getX());
class A {
protected int x = 7;
}
class B extends A {
int getX() {
return x;
}
}
System.out.println(new B().getX());
Correct!
Wrong — correct answer is .
Subclasses can access protected members, so getX() returns 7.
Q23
Which of these is the best encapsulated design?
Correct!
Wrong — correct answer is .
Private state with controlled public methods is encapsulation.
Q24
What is the output of this code?
class A {
private int x;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
A a = new A();
a.setX(99);
System.out.println(a.getX());
class A {
private int x;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
A a = new A();
a.setX(99);
System.out.println(a.getX());
Correct!
Wrong — correct answer is .
The setter stores 99 and the getter returns it.
Q25
What happens when a method is public?
Correct!
Wrong — correct answer is .
public gives universal access.
Q26
What does the final keyword on a field do?
Correct!
Wrong — correct answer is .
final fields can be assigned once, acting as constants.
Q27
What is the output of this code?
class A {
public static final int MAX = 100;
}
System.out.println(A.MAX);
class A {
public static final int MAX = 100;
}
System.out.println(A.MAX);
Correct!
Wrong — correct answer is .
Static final constants are accessed via the class name.
Q28
Which combination describes a constant shared by all instances?
Correct!
Wrong — correct answer is .
static final fields are class-level constants.
Q29
What is the output of this code?
class A {
private int x = 1;
public A() {
x = 2;
}
public int getX() {
return x;
}
}
System.out.println(new A().getX());
class A {
private int x = 1;
public A() {
x = 2;
}
public int getX() {
return x;
}
}
System.out.println(new A().getX());
Correct!
Wrong — correct answer is .
The constructor updates the private field to 2.
Q30
Why is exposing fields directly discouraged?
Correct!
Wrong — correct answer is .
Direct field writes bypass validation, risking invalid state.
Q31
What is a POJO?
Correct!
Wrong — correct answer is .
POJOs are simple classes typically following JavaBean-style encapsulation.