Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Constructors and Object Initialization
What will be the output of this code?
class A:
    def __init__(self, x):
        self.x = x
    def show(self):
        print(self.x)

obj = A(10)
obj.show()
Aself.x
BError: missing argument
C10
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the constructor

    The constructor __init__ sets self.x to 10 when obj = A(10) is called.
  2. Step 2: Understand the show method

    The show method prints self.x, which is 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Output of obj.show() = 10 [OK]
Quick Trick: Instance variables set in __init__ accessed via self [OK]
Common Mistakes:
  • Expecting self.x to print literally
  • Forgetting to pass argument to constructor
  • Confusing method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes