Bird
Raised Fist0

You want to extend a parent class method calculate so that the child class adds 10 to the parent's result. Which code correctly does this?

hard🚀 Application Q15 of Q15
Python - Inheritance and Code Reuse
You want to extend a parent class method calculate so that the child class adds 10 to the parent's result. Which code correctly does this?
class Parent:
    def calculate(self):
        return 5

class Child(Parent):
    def calculate(self):
        # Fill here
Areturn Parent.calculate() + 10
Breturn calculate() + 10
Creturn super().calculate() + 10
Dreturn self.calculate() + 10
Step-by-Step Solution
Solution:
  1. Step 1: Use super() to call parent method

    To get the parent's result, call super().calculate() inside the child method.
  2. Step 2: Add 10 to the parent's result

    Return the parent's value plus 10 as super().calculate() + 10.
  3. Final Answer:

    return super().calculate() + 10 -> Option C
  4. Quick Check:

    super() calls parent, add 10 = C [OK]
Quick Trick: Use return super().method() + extra to extend result [OK]
Common Mistakes:
MISTAKES
  • Calling calculate() without super causes recursion
  • Calling Parent.calculate() without instance
  • Using self.calculate() causes infinite loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes