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 Test:
    def __init__(self):
        self._value = 100

    def print_value(self):
        print(self.value)

t = Test()
t.print_value()
AAttributeError because self.value does not exist
BPrints 100 correctly
CSyntaxError due to missing underscore
DTypeError in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute names

    The attribute is named _value, but the method tries to print self.value without underscore.
  2. Step 2: Understand attribute access

    Since self.value is not defined, accessing it causes an AttributeError.
  3. Final Answer:

    AttributeError because self.value does not exist -> Option A
  4. Quick Check:

    Attribute names must match exactly including underscores [OK]
Quick Trick: Attribute names must match exactly including underscores [OK]
Common Mistakes:
  • Ignoring underscore in attribute name
  • Expecting print to work without error
  • Confusing attribute names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes