Bird
Raised Fist0

Given this code, what will be printed?

hard🚀 Application Q9 of Q15
Python - Methods and Behavior Definition
Given this code, what will be printed?
class A:
    def greet(self):
        return 'Hello from A'

class B(A):
    def greet(self):
        return super().greet() + ' and B'

b = B()
print(b.greet())
AHello from A
BHello from B
CError: super() not defined
DHello from A and B
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding and super()

    B's greet calls A's greet via super(), then adds ' and B'.
  2. Step 2: Combine returned strings

    super().greet() returns 'Hello from A', concatenated with ' and B'.
  3. Final Answer:

    Hello from A and B -> Option D
  4. Quick Check:

    super() calls parent method = C [OK]
Quick Trick: super() calls parent method in inheritance [OK]
Common Mistakes:
MISTAKES
  • Ignoring super() call
  • Expecting only child method output
  • Thinking super() causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes