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?Solution
The getBalance method allows indirect access to the balance variable. The BankAccount class demonstrates encapsulation, one of the key principles of Object-Oriented Programming (OOP). Encapsulation refers to the practice of hiding the internal state of an object and allowing access to it only through public methods. In this case, the balance variable is declared as private, meaning it cannot be accessed directly from outside the class. However, the class provides a public getBalance method, which allows indirect access to the balance by returning its value.The getBalance method maintains the principle of encapsulation because it provides controlled access to the internal state (balance) of the object. This way, the class maintains control over how the balance is accessed and modified. This ensures that only valid operations (e.g., checking the balance, depositing, and withdrawing) can be performed, protecting the integrity of the balance variable. Why Other Options Are Wrong: Option 1: The balance variable can be directly accessed and modified outside the class.This option is incorrect because the balance variable is declared as private, which means it cannot be accessed or modified directly from outside the BankAccount class. Any access to the balance must be done through the provided public methods (getBalance, deposit, withdraw). Option 2: The BankAccount class does not follow the principle of encapsulation because the methods are public.This option misunderstands encapsulation. Encapsulation is about protecting the internal state by making variables private and exposing only necessary behavior through public methods. The BankAccount class follows this principle because the balance variable is private, and access is controlled through the public methods. Hence, this option is incorrect. Option 3: The withdraw method can reduce the balance to a negative value.This option is incorrect because the withdraw method checks that the amount to be withdrawn is less than or equal to the current balance. If the withdrawal amount exceeds the balance, the operation is not performed, ensuring that the balance does not become negative. Option 5: The deposit method does not check for negative amounts, allowing invalid deposits.This option is incorrect because the deposit method includes a check to ensure that only positive amounts are deposited. If the amount is negative or zero, the deposit is not processed. Hence, invalid deposits are prevented by the method.
Six persons S, T, U, V, W, and X live on different floors of a six-floor building. The bottommost floor is numbered as 1 and the topmost floor is number...
Five person P, Q, R, S and T all are sitting around a circle. Who are sitting around the circular table facing towards the centre?
I. S sits to ...
How is A related to B?
I Â C has only two kids A & B and is father-in-law of D who is brother-in-law of B
II Â R is brother-in-law of A ...
What is Didu’s rank in the class?
Statement I:         Didu’s rank from the bottom is 49th and there are 50 students in ...
Which direction Mita is facing?
Statement I: Â Mita started walking from point P.
Statement II: She took two consecutive right turns.
O, P, Q, S, T and V are standing in straight line facing north. Who among the following is between S and V?
I. V stand second to the right of S. ...
How is ‘feedback’ written in a code language?
I. In a certain code language ‘corporate office branch centre’ is written as ‘ki pa mt  ...
Five friends Harsha, Geeta, Naman, Suraj and Anuj are seated around a circular table facing the centre. Who sits to the immediate right of Suraj?
How is B related to C?
I. C and D are the sons of E. B is son of E’s father.
II. E is sister of B and mother of D, who is brother of C.
The question given below consists of two statements numbered I and II given below it. You have to decide whether the data provided in the statements a...