Bird
0
0

Identify the problem in this code:

medium📝 Debug Q7 of 15
Python - Encapsulation and Data Protection
Identify the problem in this code:
class Car():
    def __init__(self, model):
        self.__model = model

c = Car('Tesla')
print(c.__model)
ANo problem; prints 'Tesla'
BMissing parentheses in print
CModel attribute not assigned
DCannot access __model directly; it's private
Step-by-Step Solution
Solution:
  1. Step 1: Understand double underscore attribute naming

    Attributes starting with double underscores are private and name-mangled.
  2. Step 2: Accessing private attribute directly causes error

    Trying to print c.__model raises an AttributeError.
  3. Final Answer:

    Cannot access __model directly; it's private -> Option D
  4. Quick Check:

    Double underscore attributes are private [OK]
Quick Trick: Double underscore means private; can't access directly [OK]
Common Mistakes:
  • Trying to access private attributes directly
  • Confusing single and double underscore
  • Assuming no error on private attribute access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes