Bird
0
0

Identify the error in this code:

medium📝 Debug Q7 of 15
Python - Constructors and Object Initialization
Identify the error in this code:
class D:
    def __init__(self, val):
        self.val = val
    def show():
        print(self.val)

d = D(5)
d.show()
AIncorrect constructor syntax
BMissing <code>self</code> parameter in <code>show</code> method
CInstance variable <code>val</code> not defined
DCannot call method without arguments
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions

    All instance methods must have self as the first parameter.
  2. Step 2: Identify missing self

    The show method lacks self, so it cannot access instance variables.
  3. Final Answer:

    Missing self parameter in show method -> Option B
  4. Quick Check:

    Instance methods need self parameter [OK]
Quick Trick: Always include self in instance method parameters [OK]
Common Mistakes:
  • Omitting self in method definitions
  • Trying to access instance variables without self
  • Confusing static and instance methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes