|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - quiz |
| 4 | +created: 2026-02-11 |
| 5 | +title: Quiz 2 |
| 6 | +--- |
| 7 | +All the Question are based on the pattern [[Singleton Pattern]], [[Command Pattern]], [[Adapter Pattern]], [[Facade Pattern]], [[Iterator Pattern]], [[Composite Pattern]], [[State Pattern]], [[Proxy Pattern]] & [[Compound Pattern]]. |
| 8 | +#### Question 1 |
| 9 | + |
| 10 | +You are building an **expense dashboard app (like your CashFlow project)**. |
| 11 | +The application needs **one shared database connection** so that: |
| 12 | +- All modules use the same connection |
| 13 | +- Creating multiple connections would waste memory and slow the system |
| 14 | + |
| 15 | +**Which design pattern is most appropriate here, and why?** |
| 16 | +> [!FAQ]- Answer and Explanation |
| 17 | +> **Pattern:** Singleton |
| 18 | +> **Explanation:** |
| 19 | +> A database connection is a classic **Singleton** use case because: |
| 20 | +> - Only **one instance** is needed across the application |
| 21 | +> - It provides a **global access point** |
| 22 | +> - Prevents unnecessary memory usage and connection overhead |
| 23 | +
|
| 24 | +--- |
| 25 | +#### Question 2 |
| 26 | + |
| 27 | +You are designing a **remote control app** for smart home devices. |
| 28 | +Each button can: |
| 29 | +- Turn lights ON/OFF |
| 30 | +- Open/close curtains |
| 31 | +- Start the fan |
| 32 | + |
| 33 | +You also want to support **Undo** functionality. |
| 34 | +**Which design pattern is most appropriate here, and why?** |
| 35 | +> [!FAQ]- Answer and Explanation |
| 36 | +> **Pattern:** Command |
| 37 | +> **Explanation:** The **Command Pattern** encapsulates each action as an object. |
| 38 | +> This allows: |
| 39 | +> - Storing commands |
| 40 | +> - Undoing operations |
| 41 | +> - Decoupling the invoker (remote) from receivers (devices) |
| 42 | +
|
| 43 | +--- |
| 44 | +#### Question 3 |
| 45 | + |
| 46 | +You are integrating a **payment gateway** into an e-commerce system. |
| 47 | +Your system expects a **standard payment interface**, but the new gateway provides a **different API** that cannot be changed. |
| 48 | + |
| 49 | +**Which design pattern is most appropriate here, and why?** |
| 50 | +> [!FAQ]- Answer and Explanation |
| 51 | +> **Pattern:** Adapter |
| 52 | +> **Explanation:** The **Adapter Pattern** converts one interface into another expected by the client. |
| 53 | +> It allows integration of incompatible systems without modifying existing code. |
| 54 | +
|
| 55 | +--- |
| 56 | +#### Question 4 |
| 57 | + |
| 58 | +You are building a **movie streaming app**. |
| 59 | +Playing a movie involves: |
| 60 | +- Authentication |
| 61 | +- Loading subtitles |
| 62 | +- Initializing decoder |
| 63 | +- Buffering video |
| 64 | + |
| 65 | +You want a simple method like: |
| 66 | +`playMovie()` |
| 67 | + |
| 68 | +**Which design pattern is most appropriate here, and why?** |
| 69 | +> [!FAQ]- Answer and Explanation |
| 70 | +> **Pattern:** Facade |
| 71 | +> **Explanation:** The **Facade Pattern** provides a simplified interface to a complex subsystem, hiding internal operations from the user. |
| 72 | +
|
| 73 | +--- |
| 74 | +#### Question 5 |
| 75 | + |
| 76 | +You are developing a **music playlist app**. |
| 77 | +Users should move through songs without knowing whether songs are stored in arrays, lists, or databases. |
| 78 | + |
| 79 | +**Which design pattern is most appropriate here, and why?** |
| 80 | +> [!FAQ]- Answer and Explanation |
| 81 | +> **Pattern:** Iterator |
| 82 | +> **Explanation:** The **Iterator Pattern** provides a standard way to traverse a collection without exposing its internal structure. |
| 83 | +
|
| 84 | +--- |
| 85 | +#### Question 6 |
| 86 | + |
| 87 | +You are building a **file explorer system** where: |
| 88 | +- A folder can contain files and other folders |
| 89 | +- Operations like `delete()` or `showSize()` should work uniformly |
| 90 | + |
| 91 | +**Which design pattern is most appropriate here, and why?** |
| 92 | +> [!FAQ]- Answer and Explanation |
| 93 | +> **Pattern:** Composite |
| 94 | +> **Explanation:** The **Composite Pattern** allows treating individual objects and groups uniformly in tree structures. |
| 95 | +
|
| 96 | +--- |
| 97 | +#### Question 7 |
| 98 | + |
| 99 | +You are designing a **document editor** where documents can be: |
| 100 | +- Draft |
| 101 | +- Review |
| 102 | +- Published |
| 103 | +Each state changes editing behavior. |
| 104 | + |
| 105 | +**Which design pattern is most appropriate here, and why?** |
| 106 | +> [!FAQ]- Answer and Explanation |
| 107 | +> **Pattern:** State |
| 108 | +> **Explanation:** The **State Pattern** allows behavior to change dynamically based on the object's internal state. |
| 109 | +
|
| 110 | +--- |
| 111 | +#### Question 8 |
| 112 | + |
| 113 | +You are developing an **image viewer** where large images should load only when opened, while placeholders are shown initially. |
| 114 | + |
| 115 | +**Which design pattern is most appropriate here, and why?** |
| 116 | +> [!FAQ]- Answer and Explanation |
| 117 | +> **Pattern:** Proxy |
| 118 | +> **Explanation:** A **Virtual Proxy** delays object creation until needed, improving performance and memory usage. |
| 119 | +
|
| 120 | +--- |
| 121 | +#### Question 9 |
| 122 | + |
| 123 | +You are integrating a **complex third-party payment library** and create a simple class exposing only a few methods like `startPayment()` and `verifyPayment()`. |
| 124 | + |
| 125 | +**Which design pattern is most appropriate here, and why?** |
| 126 | +> [!FAQ]- Answer and Explanation |
| 127 | +> **Pattern:** Facade |
| 128 | +> **Explanation:** The **Facade Pattern** simplifies interaction with complex subsystems by providing a unified interface. |
| 129 | +
|
| 130 | +--- |
| 131 | +#### Question 10 |
| 132 | + |
| 133 | +You are building a **text editor** that supports: |
| 134 | +- Copy |
| 135 | +- Paste |
| 136 | +- Cut |
| 137 | +- Undo / Redo |
| 138 | +Operations must be stored and reversible. |
| 139 | + |
| 140 | +**Which design pattern is most appropriate here, and why?** |
| 141 | +> [!FAQ]- Answer and Explanation |
| 142 | +> **Pattern:** Command |
| 143 | +> **Explanation:** Commands encapsulate operations as objects, enabling undo/redo and history tracking. |
| 144 | +
|
| 145 | +--- |
| 146 | +#### Question 11 |
| 147 | + |
| 148 | +You are designing a **company organizational hierarchy** where: |
| 149 | +- Managers contain employees |
| 150 | +- Employees and managers should be treated uniformly |
| 151 | + |
| 152 | +**Which design pattern is most appropriate here, and why?** |
| 153 | +> [!FAQ]- Answer and Explanation |
| 154 | +> **Pattern:** Composite |
| 155 | +> **Explanation:** Composite models hierarchical structures and allows uniform treatment of individuals and groups. |
| 156 | +
|
| 157 | +--- |
| 158 | +#### Question 12 |
| 159 | + |
| 160 | +A banking system performs authentication and validation before forwarding requests to the real transfer service. |
| 161 | + |
| 162 | +**Which design pattern is most appropriate here, and why?** |
| 163 | +> [!FAQ]- Answer and Explanation |
| 164 | +> **Pattern:** Proxy |
| 165 | +> **Explanation:** A **Protection Proxy** controls access to another object by performing checks before forwarding requests. |
| 166 | +
|
| 167 | +--- |
| 168 | +#### Question 13 |
| 169 | + |
| 170 | +You are designing a **vending machine** where behavior changes depending on: |
| 171 | +- Coin inserted |
| 172 | +- Item selected |
| 173 | +- Item dispensed |
| 174 | + |
| 175 | +**Which design pattern is most appropriate here, and why?** |
| 176 | +> [!FAQ]- Answer and Explanation |
| 177 | +> **Pattern:** State |
| 178 | +> **Explanation:** Each state defines behavior, eliminating large conditional logic and making transitions easier. |
| 179 | +
|
| 180 | +--- |
| 181 | +#### Question 14 |
| 182 | + |
| 183 | +Your application expects a logging interface, but a **legacy logging system** has a different method signature. |
| 184 | + |
| 185 | +**Which design pattern is most appropriate here, and why?** |
| 186 | +> [!FAQ]- Answer and Explanation |
| 187 | +> **Pattern:** Adapter |
| 188 | +> **Explanation:** Adapter translates one interface into another compatible interface without modifying existing systems. |
| 189 | +
|
| 190 | +--- |
| 191 | +#### Question 15 |
| 192 | + |
| 193 | +You are building a **graphics editor** where: |
| 194 | +- Shapes can be individual or grouped |
| 195 | +- Calling `draw()` should work for both |
| 196 | + |
| 197 | +**Which design pattern is most appropriate here, and why?** |
| 198 | +> [!FAQ]- Answer and Explanation |
| 199 | +> **Pattern:** Composite |
| 200 | +> **Explanation:** Composite allows treating single objects and groups uniformly in tree structures. |
| 201 | +
|
| 202 | +--- |
| 203 | +#### Question 16 |
| 204 | + |
| 205 | +A **library management system** stores books in multiple structures but must allow uniform traversal. |
| 206 | + |
| 207 | +**Which design pattern is most appropriate here, and why?** |
| 208 | +> [!FAQ]- Answer and Explanation |
| 209 | +> **Pattern:** Iterator |
| 210 | +> **Explanation:** Iterator enables traversal without exposing internal storage details. |
| 211 | +
|
| 212 | +--- |
| 213 | +#### Question 17 |
| 214 | + |
| 215 | +You are designing a **configuration manager** that must: |
| 216 | +- Load configuration once |
| 217 | +- Share it globally |
| 218 | + |
| 219 | +**Which design pattern is most appropriate here, and why?** |
| 220 | +> [!FAQ]- Answer and Explanation |
| 221 | +> **Pattern:** Singleton |
| 222 | +> **Explanation:** Singleton ensures a single shared instance and controlled access. |
| 223 | +
|
| 224 | +--- |
| 225 | +#### Question 18 |
| 226 | + |
| 227 | +A **video streaming platform** uses a local cache server that decides whether to fetch from cache or the main server. |
| 228 | + |
| 229 | +**Which design pattern is most appropriate here, and why?** |
| 230 | +> [!FAQ]- Answer and Explanation |
| 231 | +> **Pattern:** Proxy |
| 232 | +> **Explanation:** A caching proxy controls access and improves performance by serving cached content. |
| 233 | +
|
| 234 | +--- |
| 235 | +#### Question 19 |
| 236 | + |
| 237 | +A **smart home system** combines: |
| 238 | +- Command for actions |
| 239 | +- Composite for device groups |
| 240 | +- Iterator for traversal |
| 241 | +- Facade for simplified control |
| 242 | + |
| 243 | +**What is this approach called?** |
| 244 | +> [!FAQ]- Answer and Explanation |
| 245 | +> **Pattern:** Compound Pattern |
| 246 | +> **Explanation:** Compound patterns combine multiple design patterns to solve complex design problems. |
| 247 | +
|
| 248 | +--- |
| 249 | +#### Question 20 |
| 250 | + |
| 251 | +A **game character system** has states like: |
| 252 | +- Standing |
| 253 | +- Running |
| 254 | +- Jumping |
| 255 | +- Attacking |
| 256 | +Behavior changes dynamically with state. |
| 257 | + |
| 258 | +**Which design pattern is most appropriate here, and why?** |
| 259 | +> [!FAQ]- Answer and Explanation |
| 260 | +> **Pattern:** State |
| 261 | +> **Explanation:** The State Pattern encapsulates state-specific behavior into separate classes and enables dynamic transitions. |
| 262 | +
|
0 commit comments