🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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" ...?

  1. A.A longer if-else chain
  2. B.A switch statement on the type name
  3. C.Storing the type as an integer
  4. 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 →