0
0
Javaprogramming~20 mins

Encapsulation best practices in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Encapsulation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Encapsulation with Private Fields
What is the output of this Java code that uses encapsulation with private fields and public getters/setters?
Java
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0) {
            this.age = age;
        }
    }

    public static void main(String[] args) {
        Person p = new Person("Alice", 30);
        p.setAge(-5);
        System.out.println(p.getName() + " is " + p.getAge() + " years old.");
    }
}
ACompilation error due to private fields.
BAlice is 30 years old.
CAlice is -5 years old.
DRuntime error due to invalid age.
Attempts:
2 left
πŸ’‘ Hint
Check how the setAge method handles negative values.
🧠 Conceptual
intermediate
1:30remaining
Why Use Private Fields in Encapsulation?
Why is it a best practice to declare class fields as private when using encapsulation in Java?
ATo allow subclasses to access fields directly without getters or setters.
BTo make the fields accessible from any class without restrictions.
CTo prevent direct access and modification from outside the class, ensuring control over data.
DTo improve the speed of the program by avoiding method calls.
Attempts:
2 left
πŸ’‘ Hint
Think about how encapsulation protects data integrity.
πŸ”§ Debug
advanced
2:00remaining
Identify the Encapsulation Violation
Which option shows a violation of encapsulation best practices in Java?
Java
public class BankAccount {
    public double balance;

    public BankAccount(double balance) {
        this.balance = balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}
AMaking the balance field public allows direct modification from outside the class.
BProviding a deposit method to update balance.
CChecking if deposit amount is positive before adding.
DUsing a constructor to set the initial balance.
Attempts:
2 left
πŸ’‘ Hint
Encapsulation means hiding data from outside direct access.
πŸ“ Syntax
advanced
1:30remaining
Correct Setter Method Syntax for Encapsulation
Which setter method correctly follows encapsulation best practices in Java?
Java
private int score;
Apublic void setScore(int score) { this.score = score; }
Bpublic int setScore(int score) { this.score = score; return score; }
Cpublic void setScore() { score = 10; }
Dvoid setScore(int score) { score = score; }
Attempts:
2 left
πŸ’‘ Hint
A setter should accept a parameter and update the private field correctly.
πŸš€ Application
expert
2:00remaining
Number of Accessible Fields After Encapsulation
Given this class, how many fields are accessible directly from outside the class without using getters or setters?
Java
public class Car {
    private String model;
    protected int year;
    public String color;
    String owner;
}
A4
B2
C3
D1
Attempts:
2 left
πŸ’‘ Hint
Consider Java's access levels: private, protected, public, and default (package-private).