LearnFrenzy
Sign in Take a free test

Inheritance and Polymorphism · Exercise 2 of 4

Inheritance and Polymorphism Predict the Output

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output.

Question 11 Medium

What is the output of the following Python program?

main.py
class Report:
    def render(self):
        return f"[{self.header()}] {self.body()}"
    def header(self):
        return "REPORT"
    def body(self):
        return "empty"

class Sales(Report):
    def body(self):
        return "sales up 12%"

class Audit(Report):
    def header(self):
        return "AUDIT"

print(Sales().render(), "|", Audit().render())
A [REPORT] empty | [REPORT] empty
B [REPORT] sales up 12% | [REPORT] empty
C [REPORT] sales up 12% | [AUDIT] empty
D AttributeError
Question page & discussion
Question 12 Easy

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
D TypeError
Question page & discussion
Question 13 Medium

What is the output of the following Python program?

main.py
class A:
    def total(self): return 1
class B(A):
    def total(self): return super().total() + 10
class C(B):
    def total(self): return super().total() + 100

print(C().total(), B().total(), [k.__name__ for k in C.__mro__][:3])
A 111 11 ['C', 'B', 'A']
B 100 10 ['C', 'B', 'A']
C 111 11 ['A', 'B', 'C']
D 101 11 ['C', 'B', 'A']
Question page & discussion
Question 14 Hard

What is the output of the following Python program?

main.py
class Pts:
    def __init__(self, n): self.n = n
    def __add__(self, other):
        return Pts(self.n + (other.n if isinstance(other, Pts) else other))
    __radd__ = __add__
    def __repr__(self): return f"Pts({self.n})"

print(sum([Pts(5), Pts(7), Pts(3)]), Pts(1) + 4, 4 + Pts(1))
A TypeError
B Pts(15) Pts(5) 5
C Pts(15) Pts(5) Pts(5)
D 15 Pts(5) Pts(5)
Question page & discussion
Question 15 Medium

What is the output of the following Python program?

main.py
class Flyer:
    def move(self): return "fly"
class Swimmer:
    def move(self): return "swim"

class Duck(Swimmer, Flyer): pass
class Plane(Flyer): pass

print(Duck().move(), Plane().move(), Duck.__bases__[0].__name__)
A swim fly Swimmer
B fly fly Flyer
C swim swim Swimmer
D fly swim Flyer
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links