Module 3: Software implementation
Knowledge check: Coding best practices
Practice Assignment
1. A company recently implemented a new feature in its software. However, when deployed, the new feature caused an older, unrelated feature to stop working. Upon investigation, the team discovered that the developer working on the older feature had left the company, and no documentation was available to understand the feature's implementation. What could have helped avoid this situation?
- Proper documentation ✅
- Frequent refactoring
- Avoiding code comments
- Using magic numbers in code
Explanation:
Proper documentation ensures that every feature’s implementation is clearly recorded. This allows new or existing team members to understand and maintain the software effectively, even in the absence of the original developer.
2. Which practices align with clean code principles to improve software maintainability? Select all that apply.
- Meaningful and descriptive variable names ✅
- Copying code to reuse functionality
- Single Responsibility Principle ✅
- Short functions that do one thing ✅
Explanation:
- Meaningful and descriptive variable names make the code self-explanatory.
- Single Responsibility Principle ensures each class or function focuses on a specific task, reducing complexity.
- Short functions that do one thing enhance readability and testability.
3. You are tasked with deploying a Java Spring Boot application to multiple environments: development, testing, and production. Each environment requires different configurations for database connections. Which Spring feature would allow you to manage these configurations effectively?
- Spring Profiles ✅
- Dependency injection
- Custom annotations
- Java reflection API
Explanation:
Spring Profiles let you define different configurations for each environment (e.g., development, testing, production) and activate the appropriate profile during deployment. This makes managing environment-specific settings efficient.
4. Which of the following best describes the Single Responsibility Principle (SRP) in clean code?
- A function should handle multiple tasks to avoid creating too many functions.
- Code should always fail silently when an error occurs.
- All classes should have a single author.
- Every class or method should perform only one well-defined task. ✅
Explanation:
The Single Responsibility Principle emphasizes that a class or method should have a single, clear purpose. This improves code modularity and simplifies maintenance and debugging.
5. Which of the following is a key practice for ensuring long-term software maintainability?
- Avoiding knowledge transfer within the team
- Writing proper documentation comments ✅
- Neglecting to review a large and complex codebase
- Ignoring external library dependencies
Explanation:
Proper documentation comments help team members understand the codebase, facilitating easier maintenance and onboarding of new developers. Neglecting reviews or avoiding knowledge sharing leads to technical debt and reduced collaboration.
Knowledge check: Testing and debugging
Practice Assignment
6. Scenario: You are working on a high-profile e-commerce application. A customer reports that they cannot add products to their shopping cart. You’ve already verified that the product API is returning valid product details. What would be the most logical next step in identifying the root cause of this issue?
- Add breakpoints to the method that adds products in the service layer to analyze the value of input variables during execution. ✅
- Restart the application server and hope the issue resolves itself.
- Modify the database schema without confirming if the database is the actual source of the issue.
- Assume the issue is with the front-end code and immediately escalate it to the front-end team.
Explanation:
Adding breakpoints allows you to pause program execution and inspect the flow of data and logic in the service layer. This helps identify any potential issues with the method handling the addition of products to the shopping cart. Restarting the application or escalating without investigation would be premature, and modifying the database schema without evidence could cause further issues.
7. What is the primary function of breakpoints in debugging?
- To generate automated tests for the code.
- To execute all lines of code continuously.
- To replace print statements in the code.
- To pause program execution and inspect its state. ✅
Explanation:
Breakpoints are used during debugging to temporarily halt the execution of a program at a specific point, enabling developers to inspect variables, memory, and control flow. They are a crucial tool for identifying and resolving issues.
8. Which type of software testing is primarily focused on validating business and user requirements?
- Unit testing
- Integration testing
- System testing
- Acceptance testing ✅
Explanation:
Acceptance testing verifies that the software meets business and user requirements. It is usually the final stage of testing performed by the end-users or stakeholders to ensure the application satisfies their needs.
9. While testing the BookController in a Spring application, you want to verify that a book added via the POST endpoint has the correct attributes in the response. Which tool should you use?
- JUnit with no additional tools.
- A running application server to test the endpoint manually.
- JSONPath in the application’s production environment.
- MockMvc with jsonPath assertions ✅
Explanation:
MockMvc is used for testing Spring MVC controllers without starting the full application context. It allows you to simulate HTTP requests and verify responses using jsonPath to assert the presence and correctness of JSON fields in the response.
10. During service layer testing, you notice that calling the saveBook method with a duplicate ID overwrites the existing book. How can this behavior be validated?
- Avoid saving books with duplicate IDs in tests.
- Log a warning instead of throwing an exception.
- Use MockMvc to simulate duplicate book saves.
- Save multiple books with the same ID and verify that the returned book reflects the data from the most recent save operation. ✅
Explanation:
To validate the behavior, you should create a test case where multiple save operations are performed with the same ID. By checking the data in the returned book, you can confirm whether the service overwrites the existing record or handles duplicates appropriately. This approach ensures clarity in understanding and addressing the behavior.
Module quiz: Software implementation
Graded Assignment
11. Debugging is a crucial process for ensuring high-quality applications. Which of the following statements best describe the benefits and challenges of effective debugging? Select all that apply.
- Debugging eliminates the need for testing by ensuring the application works flawlessly after a fix.
- Debugging ensures that issues do not occur again by fixing root causes and improving code maintainability. ✅
- Debugging provides tools like breakpoints and variable inspection to systematically trace issues in code execution. ✅
- Debugging ensures applications deliver a smooth user experience. ✅
Explanation:
Effective debugging helps improve code maintainability by addressing root causes and using systematic tools like breakpoints to trace issues. While debugging enhances user experience, it does not eliminate the need for testing.
12. Which advanced breakpoint feature allows execution to pause only when a specific condition is met?
- Conditional breakpoints ✅
- Field watchpoints
- Method breakpoints
- Exception breakpoints
Explanation:
Conditional breakpoints pause the program execution only if a user-defined condition evaluates to true, making debugging more efficient by focusing on specific scenarios.
13. True or false: Unit testing focuses on verifying interactions between components in a system.
- True
- False ✅
Explanation:
Unit testing focuses on testing individual components or functions in isolation. Testing interactions between components is typically done during integration testing.
14. True or False: MockMvc allows developers to test REST controllers without starting a full application server.
- True ✅
- False
Explanation:
MockMvc simulates HTTP requests to REST controllers without requiring a full application server, making testing faster and more lightweight.
15. Which JUnit method is used to validate that a specific exception is thrown during service testing?
- assertEquals()
- assertTrue()
- assertThrows() ✅
- assertNotNull()
Explanation:assertThrows()
is used in JUnit to verify that a specific exception is thrown during the execution of a method.
16. True or false: The saveBook method in BookService can handle duplicate IDs without overwriting data.
- True
- False ✅
Explanation:
If the saveBook
method is overwriting existing data with duplicate IDs, it indicates that the method is not handling duplicates correctly, which can lead to data loss.
17. What is a best practice for investigating issues in large codebases?
- Add logs to every line of code to track execution.
- Test only one component at a time.
- Skip logging and focus solely on assumptions.
- Break down problems into smaller parts and debug incrementally ✅
Explanation:
Breaking down issues into smaller, manageable parts and debugging step-by-step allows for a focused and systematic resolution process. Adding excessive logs or making assumptions without data can be counterproductive.
18. Which of the following are key considerations for maintaining software effectively? Select all that apply.
- Prioritizing speed over maintainability during development.
- Ensuring proper documentation is written (e.g., using Javadoc or JSDoc). ✅
- Using software design patterns to improve maintainability. ✅
- Relying on a single developer to manage critical parts of the codebase.
Explanation:
Proper documentation and design patterns enhance code clarity and maintainability. Relying on a single developer or prioritizing speed over maintainability can lead to long-term issues.
19. A developer creates a method that validates user input, logs the data, and updates the database. Which clean code principle is being violated?
- Single Respon sibility Principle ✅
- DRY (Don’t Repeat Yourself)
- Fail Fast
- Use of meaningful names
Explanation:
The method is violating the Single Responsibility Principle (SRP) by performing multiple tasks (validation, logging, and database updates) instead of focusing on a single responsibility.
20. A team wants to deploy an application to multiple environments (development, testing, and production) without manually changing configuration files each time. What feature should they use to simplify this process?
- Environment variables
- Spring Profiles ✅
- SLF4J logging
- Dependency injection
Explanation:
Spring Profiles allow different configurations for different environments, making it easier to manage and switch between configurations without manual intervention.