CS FundamentalsVery hardProduct-company design interview
How many of these subclass changes violate the Liskov Substitution Principle? 1. The overriding method accepts a wider range of inputs than the base method. 2. The overriding method guarantees a stronger result (e.g. a sorted list where the base promised just a list). 3. A ReadOnlyList subclass throws UnsupportedOperationException from add(), which the base List contract allows callers to use. 4. The subclass requires a non-null argument where the base method accepted null.
- A. 1
- B. 2
- C. 3
- D. 4
Correct answer: B. 2
Subtypes may weaken preconditions (1) and strengthen postconditions (2). Throwing on an operation the base contract supports (3) and demanding more of callers (4) break code written against the base type, so 2 changes violate LSP.
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
- An area calculator contains if shape.type == "circle": … elif shape.type == "square": …, and every new shape requires editing this…
- 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…