IKM Software Development 2 — Questions and Answers
Question 1: What is the primary benefit of dependency injection?
- Faster compilation
- Loose coupling between components (Correct answer)
- Reduced memory usage
- Automatic error handling
Correct answer: Loose coupling between components
Dependency injection promotes loose coupling by providing dependencies externally rather than creating them internally.
Dependency injection implements Inversion of Control. Instead of a class creating its dependencies directly, they are provided through constructors or interfaces. This improves testability and follows SOLID principles.
Question 2: Which design pattern ensures a class has only one instance?
- Factory
- Observer
- Singleton (Correct answer)
- Strategy
Correct answer: Singleton
The Singleton pattern restricts instantiation to a single object with a global access point.
Singleton uses a private constructor and a static method. Thread-safe implementations use double-checked locking or enum-based singletons. While useful for shared resources, overuse creates tight coupling.
Question 3: What does the 'O' in SOLID stand for?
- Object-Oriented Principle
- Open/Closed Principle (Correct answer)
- Overhead Optimization
- Output Coupling Principle
Correct answer: Open/Closed Principle
The Open/Closed Principle states entities should be open for extension but closed for modification.
This means you can add new functionality by writing new code without altering working code. Typically achieved through abstraction, interfaces, and polymorphism.
Question 4: What is a race condition?
- Programs competing for CPU time
- A bug where behavior depends on timing of uncontrolled events (Correct answer)
- A performance benchmark
- An optimization technique
Correct answer: A bug where behavior depends on timing of uncontrolled events
A race condition occurs when behavior depends on the timing of events like two threads accessing shared data.
Race conditions occur when multiple threads access shared mutable state without synchronization. Prevention includes mutexes, semaphores, atomic operations, and lock-free data structures.
Question 5: Which version control workflow creates a branch for each feature?
- Trunk-based development
- Feature branch workflow (Correct answer)
- Centralized workflow
- Forking workflow
Correct answer: Feature branch workflow
Feature branch workflow creates a dedicated branch for each feature, keeping main stable.
Developers create a branch from main for each feature, then merge back via pull request. This enables parallel development and code review. Git Flow extends this with additional branch types.
Question 6: What is the purpose of code refactoring?
- Add new features
- Improve internal code structure without changing external behavior (Correct answer)
- Fix reported bugs
- Migrate programming languages
Correct answer: Improve internal code structure without changing external behavior
Refactoring improves internal structure, readability, and maintainability without changing functionality.
Techniques include extracting methods, renaming variables, replacing conditionals with polymorphism, and removing duplication. Automated tests are essential during refactoring.
What is the primary benefit of dependency injection?