Quiz MS and design

1 / 16
Exit Quiz

Java & Microservices
Design Patterns Quiz

One question → answers → explanation → links

Use Left/Right arrows or click to navigate

❓ Saga Pattern

In a microservices system, when should you prefer a Saga over a 2-phase commit?

Possible Answers

  • A. When all services share the same database
  • B. When transactions are long-running and span multiple services
  • C. When strong consistency is mandatory at all times
  • D. When latency must be zero

Correct Answer & Explanation

Correct: B

Saga avoids distributed locking and allows eventual consistency using compensating actions.

Links:
https://microservices.io/patterns/data/saga.html
https://martinfowler.com/articles/microservices.html

❓ Singleton

Why is a classic Singleton often dangerous in microservices?

Possible Answers

  • A. Breaks JVM startup
  • B. Causes hidden global state and test coupling
  • C. Uses too much memory
  • D. Cannot be serialized

Correct Answer & Explanation

Correct: B

Singletons introduce global mutable state, hurting testability and scalability.

Links:
https://martinfowler.com/articles/injection.html
https://www.baeldung.com/java-singleton

❓ DAO Pattern

What is the main responsibility of a DAO?

Possible Answers

  • A. Business validation
  • B. Transaction orchestration
  • C. Data persistence abstraction
  • D. API request validation

Correct Answer & Explanation

Correct: C

DAO isolates persistence logic from business logic.

Links:
https://martinfowler.com/eaaCatalog/dataMapper.html
https://www.baeldung.com/java-dao-pattern

❓ DDD

Where should most business validation live in a DDD-inspired Java service?

Possible Answers

  • A. Controller
  • B. Repository
  • C. Service / Domain layer
  • D. Database triggers

Correct Answer & Explanation

Correct: C

Services coordinate rules while keeping entities focused on invariants.

Links:
https://martinfowler.com/bliki/DomainModel.html
https://dddcommunity.org

❓ Facade Pattern

Why use a Facade in a microservice?

Possible Answers

  • A. Replace APIs
  • B. Simplify complex subsystem access
  • C. Increase coupling
  • D. Avoid DTOs

Correct Answer & Explanation

Correct: B

Facade provides a clean boundary and hides internal complexity.

Links:
https://refactoring.guru/design-patterns/facade
https://martinfowler.com/bliki/Facade.html

❓ Strategy Pattern

What problem does Strategy solve best?

Possible Answers

  • A. Object creation
  • B. Algorithm selection at runtime
  • C. Persistence abstraction
  • D. Transaction rollback

Correct Answer & Explanation

Correct: B

Strategy replaces conditionals with interchangeable behaviors.

Links:
https://refactoring.guru/design-patterns/strategy
https://www.baeldung.com/java-strategy-pattern

❓ Circuit Breaker

What is the primary goal of a circuit breaker?

Possible Answers

  • A. Increase throughput
  • B. Prevent cascading failures
  • C. Ensure ACID transactions
  • D. Replace retries

Correct Answer & Explanation

Correct: B

It fails fast and protects the system under downstream failure.

Links:
https://martinfowler.com/bliki/CircuitBreaker.html
https://resilience4j.readme.io

❓ Validation Placement

Why keep most validation in the service layer?

Possible Answers

  • A. Easier UI development
  • B. Centralized business rules
  • C. Faster DB writes
  • D. Less code

Correct Answer & Explanation

Correct: B

Service layer ensures consistency across APIs and consumers.

Links:
https://martinfowler.com/eaaCatalog/serviceLayer.html
https://www.baeldung.com/spring-service-layer