LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Predict the Output · Question 12

__str__ inherited by a subclass Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following Python program?

main.py
class Account:
    kind = "Account"
    def __init__(self, balance):
        self.balance = balance
    def __str__(self):
        return f"{self.kind}: {self.balance}"

class Savings(Account):
    kind = "Savings"

print(Account(100), Savings(500))
  • A Account: 100 Account: 500
  • B Savings: 100 Savings: 500
  • C Account: 100 Savings: 500 Correct
  • D TypeError
Correct Answer: Option C

Savings inherits __str__, but self.kind finds Savings' own class attribute.

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