




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This practice exam focuses on clean code principles and solid design patterns. It features multiple-choice questions with detailed explanations, covering topics like the single responsibility principle, open/closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion principle. The exam tests understanding of component design, refactoring techniques, and code smells, offering insights for developers aiming to write maintainable code. It also covers test-driven development and the acyclic dependencies principle. Ideal for students and professionals, it enhances knowledge of software design best practices. The questions promote critical thinking and deeper analysis, making it valuable for academic and professional growth. The format allows self-assessment and reinforcement of key concepts.
Typology: Exams
1 / 118
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which of the following best describes the Single Responsibility Principle (SRP)? A) A class should have only one public method. B) A class should have only one reason to change. C) A class should inherit from only one base class. D) A class should contain only primitive data types. Answer: B Explanation: SRP states that a class should have a single responsibility, meaning one reason to change, which keeps it focused and maintainable. Question 2. In clean code, when should a comment be preferred over refactoring? A) When the code is already perfectly clear. B) When the comment explains why something is done, not what is done. C) When the comment can replace a missing unit test. D) When the code cannot be changed due to legacy constraints. Answer: B Explanation: Comments are valuable for explaining intent or rationale that cannot be expressed by code alone; the code itself should be self‑explanatory. Question 3. Which formatting practice improves vertical readability?
A) Placing all statements on a single line. B) Using excessive blank lines between statements. C) Grouping related statements together with a single blank line separating logical sections. D) Aligning all variable names in a column. Answer: C Explanation: Logical grouping with minimal blank lines keeps the code dense enough to scan quickly while still separating concerns. Question 4. What is a primary danger of returning null from a method? A) It forces the caller to handle a checked exception. B) It can lead to NullReferenceExceptions at runtime if the caller forgets to check. C) It improves performance by avoiding object creation. D) It automatically triggers garbage collection. Answer: B Explanation: Returning null passes the responsibility of null‑checking to callers, increasing the risk of null dereference errors. Question 5. Which design pattern most directly supports the Open/Closed Principle (OCP)? A) Singleton
C) Subclass adds a new helper method. D) Subclass implements an additional interface. Answer: B Explanation: Introducing new unchecked exceptions changes the contract, breaking substitutability of the subclass for the base class. Question 8. What is the main purpose of the Interface Segregation Principle (ISP)? A) To reduce the number of interfaces in a system. B) To force all clients to implement all methods of an interface. C) To ensure clients only depend on methods they actually use. D) To merge several small interfaces into one large one. Answer: C Explanation: ISP encourages creating fine‑grained interfaces so that implementing classes are not forced to provide unused functionality. Question 9. Which statement correctly describes Dependency Inversion Principle (DIP)? A) High‑level modules should depend on low‑level modules directly. B) Both high‑level and low‑level modules should depend on abstractions. C) Abstractions should depend on concrete implementations.
D) DIP only applies to static methods. Answer: B Explanation: DIP inverts the usual dependency direction: both layers depend on abstractions, not on concrete details. Question 10. In component design, the Common Closure Principle (CCP) recommends grouping classes that: A) Are frequently reused together by many clients. B) Change for the same reason at the same time. C) Have the same visibility modifiers. D) Belong to the same namespace. Answer: B Explanation: CCP states that classes that change together should be packaged together to minimize ripple effects. Question 11. Which of the following is a characteristic of a “God object”? A) It has only one method. B) It contains many unrelated responsibilities. C) It is immutable. D) It follows the Single Responsibility Principle. Answer: B
Question 14. In Test‑Driven Development, the “Red” phase refers to: A) Writing a failing test that defines the desired behavior. B) Refactoring code to improve design. C) Running all tests to ensure they pass. D) Deploying code to production. Answer: A Explanation: The Red phase is when you write a test that initially fails, establishing a requirement before implementation. Question 15. Which of the following is NOT a benefit of using Dependency Injection (DI)? A) Increased coupling between classes. B) Easier unit testing. C) Greater flexibility in swapping implementations. D) Separation of concerns. Answer: A Explanation: DI reduces coupling by injecting abstractions rather than hard‑coding concrete dependencies.
Question 16. A method that does both querying data and modifying state violates which clean‑code guideline? A) Command/Query Separation B) DRY (Don’t Repeat Yourself) C) KISS (Keep It Simple, Stupid) D) YAGNI (You Aren’t Gonna Need It) Answer: A Explanation: Command/Query Separation states that a method should either return data (query) or change state (command), not both. Question 17. Which of the following best describes a “value object” in the context of primitive obsession? A) An object that holds a reference to a database connection. B) An immutable object representing a domain concept (e.g., Money). C) A static utility class with only static methods. D) An object that inherits from a base DTO class. Answer: B Explanation: Value objects encapsulate related primitive values, providing type safety and behavior while being immutable. Question 18. The Stable Dependencies Principle (SDP) suggests that a component should:
C) Dependencies on third‑party libraries. D) Use of static methods across components. Answer: B Explanation: ADP requires the component dependency graph to be a Directed Acyclic Graph (DAG) to avoid circular references. Question 21. Which scenario best demonstrates a violation of the Open/Closed Principle? A) Adding a new subclass to extend behavior without changing existing code. B) Modifying an existing class to add a new feature. C) Using an interface to abstract behavior. D) Overriding a virtual method in a derived class. Answer: B Explanation: Changing existing, tested code to add functionality violates OCP; instead, you should extend via new code. Question 22. In clean code, which naming practice is recommended for boolean variables? A) count B) isEnabled C) data
D) temp Answer: B Explanation: Boolean names should be phrased as predicates (e.g., isEnabled) to make their true/false nature clear. Question 23. Which of the following is a characteristic of a “stable abstraction” according to the Stable Abstractions Principle (SAP)? A) High stability and low abstractness. B) Low stability and high abstractness. C) High stability and high abstractness. D) Low stability and low abstractness. Answer: C Explanation: SAP states that stable components should be abstract, allowing them to be extended without modification. Question 24. When should you prefer composition over inheritance? A) When the relationship is “is‑a.” B) When you need to share implementation details across unrelated classes. C) When you want to avoid the fragile base class problem. D) When you need multiple inheritance. Answer: C
Question 27. Which design pattern helps to avoid “feature envy” by moving behavior to the object that owns the data? A) Factory Method B) Observer C) Command D) Visitor Answer: D Explanation: The Visitor pattern allows operations to be defined outside the data structures, reducing the need for external classes to “envy” data. Question 28. In the context of SOLID, what does the term “contract” refer to? A. The source code license. B. The set of public methods and expected behavior defined by a type. C. The build configuration file. D. The memory allocation strategy. Answer: B Explanation: A contract is the promise a type makes about its behavior, which subtypes must honor (LSP).
Question 29. Which of the following is an advantage of using the Strategy pattern? A. It eliminates the need for interfaces. B. It hard‑codes algorithms into a single class. C. It enables selecting an algorithm at runtime without modifying existing code. D. It forces all strategies to share the same internal state. Answer: C Explanation: Strategy encapsulates algorithms, allowing them to be swapped dynamically while keeping the client unchanged. Question 30. Which of the following statements about “dead code” is true? A. It improves performance because the compiler optimizes it away. B. It should be kept for future reference. C. It increases maintenance cost and should be removed. D. It is required for unit testing. Answer: C Explanation: Dead code adds clutter, can mislead developers, and increases the surface area for bugs, so it should be eliminated. Question 31. Which of the following is a violation of the Command/Query Separation principle?
B. To create a contract that multiple classes can implement, enabling polymorphism. C. To inline all methods into a single class. D. To convert an abstract class into a concrete one. Answer: B Explanation: Extract Interface defines a set of methods that can be shared, promoting decoupling and adherence to DIP. Question 34. Which of the following is an example of “primitive obsession”? A. Using a DateTime object to represent dates. B. Storing a monetary amount as two separate int fields (dollars and cents). C. Representing a monetary amount as a single decimal value without a dedicated Money class. D. Using an enum for status codes. Answer: C Explanation: Primitive obsession occurs when primitive types are used for domain concepts that deserve dedicated value objects. Question 35. In clean code, which of the following is a recommended practice for naming a collection variable? A. list1 B. data
C. customers D. tmp Answer: C Explanation: Naming collections with plural nouns (customers) conveys that the variable holds multiple items. Question 36. Which principle states that “high‑level modules should not depend on low‑level modules; both should depend on abstractions”? A. Single Responsibility Principle B. Open/Closed Principle C. Dependency Inversion Principle D. Interface Segregation Principle Answer: C Explanation: This is the exact definition of the Dependency Inversion Principle. Question 37. Which refactoring would you apply to reduce the depth of nested conditionals? A. Replace Conditional with Polymorphism B. Introduce Parameter Object C. Move Method D. Inline Class
Explanation: Mixing persistence and validation responsibilities indicates more than one reason to change. Question 40. Which of the following is true about checked exceptions in Java? A. They are ignored by the compiler. B. They must be declared in the method signature or caught. C. They are always runtime exceptions. D. They cannot be subclassed. Answer: B Explanation: Checked exceptions enforce handling at compile time via declaration or catch blocks. Question 41. Which of the following best illustrates the “Common Reuse Principle” (CRP)? A. Packaging unrelated utility classes together. B. Grouping classes that are always used together into the same component. C. Allowing cyclic dependencies between components. D. Releasing a component without versioning. Answer: B Explanation: CRP states that classes used together should be packaged together, preventing clients from depending on unnecessary code.
Question 42. Which of the following is a recommended size for a method according to clean‑code guidelines? A. No more than 5 lines. B. No more than 20–30 lines, focusing on a single level of abstraction. C. As many lines as needed to complete the task. D. Exactly 50 lines. Answer: B Explanation: Methods should be short enough to be easily understood, typically 20 – 30 lines, and maintain a single abstraction level. Question 43. Which of the following patterns helps to decouple object creation from its usage? A. Observer B. Builder C. Factory Method D. Decorator Answer: C Explanation: The Factory Method pattern encapsulates object creation, allowing callers to depend on abstractions rather than concrete classes.