CS FundamentalsModerateTCS · Infosys

An area calculator contains `if shape.type == "circle": … elif shape.type == "square": …`, and every new shape requires editing this function. Which change best satisfies the Open/Closed principle?

  1. A. Give each shape class its own area() method and call shape.area() polymorphically
  2. B. Move the if-elif chain into a separate utility class
  3. C. Replace the if-elif with a switch statement
  4. D. Store the formulas in a global dictionary that every new shape edits

Correct answer: A. Give each shape class its own area() method and call shape.area() polymorphically

With polymorphism, a new shape is a new class (extension) and the calculator never changes (closed for modification). Moving or reshaping the conditional still requires editing it for every new type.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic