Last Updated: January 3, 2026
31 quizzes
Use the correct access modifier so a field is only accessible within its own class
String password;Click an option to fill the blank:
Expose a read-only view of a private field through a method
public int () { return age; }Click an option to fill the blank:
Ensure an immutable field can only be assigned once in the class
private String id;Click an option to fill the blank:
Which best describes encapsulation in Java?
Which access modifier provides the most restricted access?
In a well-encapsulated class, how should fields typically be declared?
What is a primary benefit of data hiding?
Which statement about immutable classes is true?
Which design correctly supports data integrity for a BankAccount balance?
What happens if you expose a mutable List directly from an immutable class?
Which access level allows access from classes in the same package but not from other packages (without inheritance)?
Why might you avoid providing a setter for a field in an otherwise mutable class?
Which requirement is essential for making a class immutable?
How does encapsulation help when refactoring a class implementation?
Order the steps to create a well-encapsulated mutable class with validation on a field.
Drag and drop to reorder, or use the arrows.
Order the steps to design a simple immutable value object in Java.
Drag and drop to reorder, or use the arrows.
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());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());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());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());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());Find the encapsulation bug in this code that manages a user’s email.
Click on the line(s) that contain the bug.
class UserProfile { public String email; public UserProfile(String email) { this.email = email; } public void updateEmail(String email) { if (email != null && email.contains("@")) { this.email = email; } }}Find the bug that breaks immutability in this supposed immutable class.
Click on the line(s) that contain the bug.
import java.util.List; final class ImmutableOrder { private final List<String> items; public ImmutableOrder(List<String> items) { this.items = items; } public List<String> getItems() { return items; }}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.
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.
Complete the code for a simple immutable User class with a single read-only field.
public final class User { private final String ; public User(String ) { this. = ; } public String getName() { return name; }}Click an option to fill blank 1:
Complete the code to encapsulate the balance field with proper validation in the constructor and a getter.
public class SafeAccount { private double ; public SafeAccount(double initialBalance) { if (initialBalance >= 0) { this. = ; } } public double () { return balance; }}Click an option to fill blank 1:
Complete the code to wrap a mutable list in an unmodifiable view for an immutable class.
import java.util.Collections;import java.util.List;public final class Tags { private final List<String> tags; public Tags(List<String> source) { this.tags = Collections.(source); } public List<String> () { return tags; }}Click an option to fill blank 1:
Complete the code to hide a password field while still allowing controlled write access.
public class Credentials { private String ; public void (String newPassword) { if (newPassword != null && newPassword.length() >= 8) { this.password = ; } }}Click an option to fill blank 1:
Click the line that breaks data hiding by exposing a field directly.
Click on the line to select.
class Customer { public String name; private int age; public int getAge() { return age; }}Click the line that exposes internal mutable state from an otherwise immutable class.
Click on the line to select.
import java.util.List; final class ReadOnlyLog { private final List<String> messages; public ReadOnlyLog(List<String> messages) { this.messages = messages; } public List<String> getMessages() { return messages; }}