Bird
0
0

Which of the following is the correct way to make a variable private in a Python class?

easy📝 Syntax Q12 of 15
Python - Encapsulation and Data Protection
Which of the following is the correct way to make a variable private in a Python class?
Avariable
B_variable
C__variable
Dpublic_variable
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python private variable syntax

    In Python, prefixing a variable with double underscore __ makes it private.
  2. Step 2: Compare options

    Only __variable uses double underscore, so it is private.
  3. Final Answer:

    __variable -> Option C
  4. Quick Check:

    Double underscore = private variable [OK]
Quick Trick: Use double underscore to make variables private [OK]
Common Mistakes:
  • Using single underscore which is only a convention
  • Using no underscore which is public
  • Confusing variable names with public keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes