📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider a BankAccount class with a balance field. To

    properly encapsulate balance, it should be declared as:
    A public int balance; Correct Answer Incorrect Answer
    B protected int balance; Correct Answer Incorrect Answer
    C private int balance; Correct Answer Incorrect Answer
    D static int balance; Correct Answer Incorrect Answer
    E final int balance; Correct Answer Incorrect Answer

    Solution

    Declaring balance as private ensures that it can only be accessed and modified by methods within the BankAccount class itself. This prevents external code from directly manipulating the balance, which could lead to inconsistencies or security issues. Public getter/setter methods would then be provided for controlled access.

    Practice Next
    ask-question