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?
- A. Give each shape class its own area() method and call shape.area() polymorphically
- B. Move the if-elif chain into a separate utility class
- C. Replace the if-elif with a switch statement
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- How many of these statements are correct? 1. Dependency Inversion is a design principle: high- and low-level modules should both depend on…
- Code contains order.getCustomer().getAddress().getCity().toUpperCase(). Which principle does it most directly violate, and why does it…
- A module reads a raw record, cleans it, and passes the cleaned record to a parser whose output is passed to a validator - each step's…
- Match each scenario with its coupling type. P: a module passes a flag telling the callee which of several operations to perform. Q: a…
- A module 'Startup' opens the log file, loads configuration, connects to the database and clears the cache. These tasks are unrelated…
- A UI component subscribes to a long-lived event bus in its constructor but never unsubscribes. Screens using it are opened and closed many…