Question

Consider the following Java code that implements encapsulation: class BankAccount {     private double balance;     public BankAccount(double initialBalance) {         if (initialBalance > 0) {             this.balance = initialBalance;         } else {             this.balance = 0;         }}     public double getBalance() {         return balance;     }     public void deposit(double amount) {         if (amount > 0) {             balance += amount;         }}     public void withdraw(double amount) {         if (amount > 0 && amount             balance -= amount; Which of the following statements is TRUE about the BankAccount class?

A The balance variable can be directly accessed and modified outside the class.
B The BankAccount class does not follow the principle of encapsulation because the methods are public.
C The withdraw method can reduce the balance to a negative value.
D The getBalance method allows indirect access to the balance variable.
E The deposit method does not check for negative amounts, allowing invalid deposits.
Practice Next

Relevant for Exams:

Hey! Ask a query