Bird
0
0

You want to protect a bank account balance so it cannot be changed directly. Which encapsulation approach is best?

hard📝 Application Q15 of 15
Python - Encapsulation and Data Protection
You want to protect a bank account balance so it cannot be changed directly. Which encapsulation approach is best?
AUse a private variable for balance and provide methods to deposit and withdraw
BMake balance a public variable and change it anywhere
CUse global variables for balance
DPrint balance directly without storing it
Step-by-Step Solution
Solution:
  1. Step 1: Understand the need for protection

    Bank balance should not be changed directly to avoid mistakes or fraud.
  2. Step 2: Apply encapsulation best practice

    Use a private variable for balance and provide public methods to safely update it.
  3. Final Answer:

    Use a private variable for balance and provide methods to deposit and withdraw -> Option A
  4. Quick Check:

    Private variable + methods = safe data access [OK]
Quick Trick: Use private variables with methods to control changes [OK]
Common Mistakes:
  • Making balance public and changing it anywhere
  • Using global variables which are unsafe
  • Not controlling how balance is updated

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes