LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Predict the Output · Question 10

Inherited and overridden class attributes Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following Python program?

main.py
class Parent:
    x = 1
class Child1(Parent):
    pass
class Child2(Parent):
    x = 2

Parent.x = 10
print(Parent.x, Child1.x, Child2.x)
  • A 10 1 2
  • B 1 1 2
  • C 10 10 10
  • D 10 10 2 Correct
Correct Answer: Option D

Child1 has no x of its own and finds Parent.x (now 10); Child2 overrides it.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links