Level 2 — RecallDesign Principles

Design Principles

30 minutes40 marksprintable — key stays hidden on paper

Subject: Coding · Chapter: Design Principles Difficulty Level: 2 (Recall — definitions, standard textbook problems, short derivations) Time Limit: 30 minutes Total Marks: 40

Answer all questions. Keep answers concise; marks are awarded for correct terminology.


Q1. State what each letter of the SOLID acronym stands for. (5 marks)

Q2. Define the DRY principle in one sentence, and give one concrete example of a DRY violation and how you would fix it. (4 marks)

Q3. Explain the KISS principle and the YAGNI principle, and state the key difference between them. (4 marks)

Q4. For the following design patterns, classify each as Creational, Structural, or Behavioral: (6 marks) (a) Observer (b) Singleton (c) Adapter (d) Factory Method (e) Decorator (f) Strategy

Q5. State the Liskov Substitution Principle. Give one short example of a violation. (4 marks)

Q6. The Open/Closed Principle says software entities should be "open for ___ but closed for ___". Fill in the blanks and explain in one sentence why inheritance/polymorphism helps satisfy it. (4 marks)

Q7. Match each pattern to its intent: (5 marks)

Pattern Intent
(a) Singleton (i) Provide a surrogate/placeholder to control access to an object
(b) Factory Method (ii) Ensure a class has only one instance with a global access point
(c) Proxy (iii) Encapsulate a request as an object
(d) Command (iv) Define an interface for creating an object, letting subclasses decide the class
(e) Facade (v) Provide a unified interface to a set of interfaces in a subsystem

Q8. Define the Interface Segregation Principle and the Dependency Inversion Principle in one sentence each. (4 marks)

Q9. What is meant by Separation of Concerns? Name one architectural pattern that embodies it. (4 marks)

Answer keyMark scheme & solutions

Q1. (5 marks — 1 each)

  • S — Single Responsibility Principle
  • O — Open/Closed Principle
  • L — Liskov Substitution Principle
  • I — Interface Segregation Principle
  • D — Dependency Inversion Principle

Why: SOLID is the standard mnemonic for the five object-oriented design principles by Robert C. Martin.


Q2. (4 marks)

  • Definition (2): DRY = "Every piece of knowledge/logic must have a single, unambiguous, authoritative representation within a system" — avoid duplicating logic.
  • Example + fix (2): e.g., the same tax-calculation formula copied into three methods → extract it into a single calculateTax() function/method that all callers use.

Why: Centralizing knowledge means a change is made in exactly one place, reducing bugs and inconsistency.


Q3. (4 marks)

  • KISS (1.5): "Keep It Simple, Stupid" — prefer the simplest design that works; avoid unnecessary complexity.
  • YAGNI (1.5): "You Aren't Gonna Need It" — don't implement functionality until it is actually required.
  • Difference (1): KISS is about how you build (simplicity of the current solution); YAGNI is about whether/when to build (avoid speculative future features).

Q4. (6 marks — 1 each)

  • (a) Observer — Behavioral
  • (b) Singleton — Creational
  • (c) Adapter — Structural
  • (d) Factory Method — Creational
  • (e) Decorator — Structural
  • (f) Strategy — Behavioral

Why: Creational = object creation; Structural = composition/relationships; Behavioral = object interaction/responsibility.


Q5. (4 marks)

  • Statement (2.5): Objects of a superclass should be replaceable with objects of a subclass without breaking the correctness of the program; subtypes must honor the contract of their base type.
  • Violation example (1.5): Square extends Rectangle where setting width also changes height breaks code that assumes independent setWidth/setHeight (also the classic Penguin extends Bird with a fly() that throws).

Q6. (4 marks)

  • Blanks (2): open for extension, closed for modification.
  • Explanation (2): You can add new behavior by creating new subclasses/implementations of an abstraction and injecting them polymorphically, so existing tested code need not be edited.

Q7. (5 marks — 1 each)

  • (a) Singleton → (ii)
  • (b) Factory Method → (iv)
  • (c) Proxy → (i)
  • (d) Command → (iii)
  • (e) Facade → (v)

Q8. (4 marks — 2 each)

  • ISP: Clients should not be forced to depend on interfaces (methods) they do not use; prefer many small, specific interfaces over one large general-purpose one.
  • DIP: High-level modules should not depend on low-level modules; both should depend on abstractions, and abstractions should not depend on details.

Q9. (4 marks)

  • Definition (3): Separation of Concerns = dividing a program into distinct sections, each addressing a separate concern/responsibility, so they can be developed and changed independently.
  • Pattern (1): e.g., MVC (Model-View-Controller), layered architecture, or MVVM.

[
  {"claim": "SOLID has exactly 5 principles", "code": "principles = ['Single Responsibility','Open/Closed','Liskov Substitution','Interface Segregation','Dependency Inversion']; result = (len(principles)==5)"},
  {"claim": "Pattern classifications for Q4 are correct counts: 2 Creational, 2 Structural, 2 Behavioral", "code": "cls = {'Observer':'B','Singleton':'C','Adapter':'S','Factory Method':'C','Decorator':'S','Strategy':'B'}; from collections import Counter; c = Counter(cls.values()); result = (c['C']==2 and c['S']==2 and c['B']==2)"},
  {"claim": "Q7 matching is a valid one-to-one bijection", "code": "match = {'Singleton':'ii','Factory Method':'iv','Proxy':'i','Command':'iii','Facade':'v'}; result = (len(set(match.values()))==5 and set(match.values())=={'i','ii','iii','iv','v'})"},
  {"claim": "Total marks sum to 40", "code": "marks=[5,4,4,6,4,4,5,4,4]; result = (sum(marks)==40)"}
]