AlgoMaster Logo

Encapsulation - Quiz

Last Updated: January 3, 2026

1 min read

Encapsulation Exercises

31 quizzes

1
Code Completion

Use the correct access modifier so a field is only accessible within its own class

java
1
String password;

Click an option to fill the blank:

2
Code Completion

Expose a read-only view of a private field through a method

java
1
public int () { return age; }

Click an option to fill the blank:

3
Code Completion

Ensure an immutable field can only be assigned once in the class

java
1
private String id;

Click an option to fill the blank:

4
Multiple Choice

Which best describes encapsulation in Java?

5
Multiple Choice

Which access modifier provides the most restricted access?

6
Multiple Choice

In a well-encapsulated class, how should fields typically be declared?

7
Multiple Choice

What is a primary benefit of data hiding?

8
Multiple Choice

Which statement about immutable classes is true?

9
Multiple Choice

Which design correctly supports data integrity for a BankAccount balance?

10
Multiple Choice

What happens if you expose a mutable List directly from an immutable class?

11
Multiple Choice

Which access level allows access from classes in the same package but not from other packages (without inheritance)?

12
Multiple Choice

Why might you avoid providing a setter for a field in an otherwise mutable class?

13
Multiple Choice

Which requirement is essential for making a class immutable?

14
Multiple Choice

How does encapsulation help when refactoring a class implementation?

15
Sequencing

Order the steps to create a well-encapsulated mutable class with validation on a field.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to design a simple immutable value object in Java.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using encapsulated access?

1class User {
2    private String name;
3    public User(String name) { this.name = name; }
4    public String getName() { return name; }
5}
6User u = new User("Alice");
7System.out.println(u.getName());
18
Output Prediction

What does this code print about a bank account balance?

1class BankAccount {
2    private double balance;
3    public BankAccount(double initialBalance) {
4        if (initialBalance >= 0) {
5            this.balance = initialBalance;
6        }
7    }
8    public double getBalance() { return balance; }
9}
10BankAccount acc = new BankAccount(-100);
11System.out.println(acc.getBalance());
19
Output Prediction

Predict the output of this code using an immutable Point class.

1final class Point {
2    private final int x;
3    private final int y;
4    public Point(int x, int y) { this.x = x; this.y = y; }
5    public int getX() { return x; }
6    public int getY() { return y; }
7}
8Point p = new Point(3, 4);
9System.out.println(p.getX() + "," + p.getY());
20
Output Prediction

What is printed when trying to change a final field through a method?

1final class Token {
2    private final String value;
3    public Token(String value) { this.value = value; }
4    public String getValue() { return value; }
5}
6Token t = new Token("abc");
7System.out.println(t.getValue());
21
Output Prediction

What is the output when using a getter after an attempted invalid deposit is blocked?

1class Account {
2    private double balance;
3    public Account(double initial) { if (initial >= 0) balance = initial; }
4    public void deposit(double amount) {
5        if (amount > 0) {
6            balance += amount;
7        }
8    }
9    public double getBalance() { return balance; }
10}
11Account a = new Account(100);
12a.deposit(-50);
13System.out.println(a.getBalance());
22
Bug Spotting

Find the encapsulation bug in this code that manages a user’s email.

Click on the line(s) that contain the bug.

java
1
class UserProfile {
2
    public String email;
3
    public UserProfile(String email) {
4
        this.email = email;
5
    }
6
    public void updateEmail(String email) {
7
        if (email != null && email.contains("@")) {
8
            this.email = email;
9
        }
10
    }
11
}
23
Bug Spotting

Find the bug that breaks immutability in this supposed immutable class.

Click on the line(s) that contain the bug.

java
1
import java.util.List;
2
 
3
final class ImmutableOrder {
4
    private final List<String> items;
5
 
6
    public ImmutableOrder(List<String> items) {
7
        this.items = items;
8
    }
9
 
10
    public List<String> getItems() {
11
        return items;
12
    }
13
}
24
Matching

Match each access modifier with its visibility description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each encapsulation-related concept with its main goal.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code for a simple immutable User class with a single read-only field.

java
1
public final class User {
2
private final String ;
3
4
public User(String ) {
5
this. = ;
6
}
7
8
public String getName() {
9
return name;
10
}
11
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to encapsulate the balance field with proper validation in the constructor and a getter.

java
1
public class SafeAccount {
2
private double ;
3
4
public SafeAccount(double initialBalance) {
5
if (initialBalance >= 0) {
6
this. = ;
7
}
8
}
9
10
public double () {
11
return balance;
12
}
13
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to wrap a mutable list in an unmodifiable view for an immutable class.

java
1
import java.util.Collections;
2
import java.util.List;
3
4
public final class Tags {
5
private final List<String> tags;
6
7
public Tags(List<String> source) {
8
this.tags = Collections.(source);
9
}
10
11
public List<String> () {
12
return tags;
13
}
14
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to hide a password field while still allowing controlled write access.

java
1
public class Credentials {
2
private String ;
3
4
public void (String newPassword) {
5
if (newPassword != null && newPassword.length() >= 8) {
6
this.password = ;
7
}
8
}
9
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that breaks data hiding by exposing a field directly.

Click on the line to select.

java
1
class Customer {
2
    public String name;
3
    private int age;
4
 
5
    public int getAge() {
6
        return age;
7
    }
8
}
31
Hotspot Selection

Click the line that exposes internal mutable state from an otherwise immutable class.

Click on the line to select.

java
1
import java.util.List;
2
 
3
final class ReadOnlyLog {
4
    private final List<String> messages;
5
 
6
    public ReadOnlyLog(List<String> messages) {
7
        this.messages = messages;
8
    }
9
 
10
    public List<String> getMessages() {
11
        return messages;
12
    }
13
}