← Quiz: Polymorphism: all 10 questions
Quiz: Polymorphism · Question 10 of 10
Which is the best polymorphic replacement for if type == "circle" ... elif type == "square" ...?
Which is the best polymorphic replacement for if type == "circle" ... elif type == "square" ...?
- A.A longer if-else chain
- B.A switch statement on the type name
- C.Storing the type as an integer
- D.Give each shape class its own area() method and call shape.area()
Answer & explanation
Correct answer: Give each shape class its own area() method and call shape.area()
Each object runs its own version, so new shapes need no change to the calling code.
Can you get the rest right without peeking?
Take the timed Quiz: Polymorphism quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →