NestJS Quiz ( Advance ) - All Questions
This advanced NestJS quiz is designed for senior backend developers and architects preparing for high-level interviews. It focuses on NestJS internals, dependency injection mechanics, request lifecycle, performance optimization, microservices, GraphQL, security, testing strategies, and real-world production scenarios.
Question 1: Why is dependency injection central to NestJS architecture?
- It improves routing speed
- It enables loose coupling and testability
- It replaces middleware
- It simplifies HTTP adapters
Explanation: DI promotes loose coupling and easier testing.
Question 2: What happens internally when a request-scoped provider is used?
- A single instance is reused
- A new instance is created per request context
- It becomes transient
- It is cached globally
Explanation: Request-scoped providers are created per request.
Question 3: What is the biggest performance drawback of request-scoped providers?
- Memory leaks
- Increased instantiation overhead
- Broken dependency graph
- Lost singleton behavior
Explanation: They increase object creation cost per request.
Question 4: Why are transient providers rarely recommended?
- They break routing
- They create new instances on every injection
- They disable DI
- They prevent async operations
Explanation: Transient providers create many instances.
Question 5: What does the NestJS injector resolve first?
- Controllers
- Providers dependencies
- Modules
- Middleware
Explanation: Dependencies are resolved before instantiation.
Question 6: What problem does forwardRef() truly solve?
- Async loading
- Circular dependency resolution
- Lazy loading modules
- Provider scope conflicts
Explanation: forwardRef resolves circular dependencies.
Question 7: Which layer executes first in the NestJS request lifecycle?
- Guards
- Middleware
- Interceptors
- Pipes
Explanation: Middleware executes before guards.
Question 8: Which layer can short-circuit a request before reaching the controller?
- Pipes
- Interceptors
- Guards
- Filters
Explanation: Guards can block execution.
Question 9: Why should business logic not be placed inside controllers?
- Performance issues
- Testing complexity and poor separation of concerns
- Routing limitations
- Decorator conflicts
Explanation: Logic belongs in services for testability.
Question 10: Which NestJS feature is best for cross-cutting concerns?
- Controllers
- Interceptors
- Providers
- Modules
Explanation: Interceptors handle cross-cutting logic.
Question 11: What is the key difference between middleware and interceptors?
- Execution order
- Interceptors support RxJS streams
- Middleware supports DI
- Interceptors run only once
Explanation: Interceptors can manipulate streams.
Question 12: Why are pipes preferred over manual validation?
- Better performance
- Centralized validation and transformation
- Less code
- Async support
Explanation: Pipes centralize validation logic.
Question 13: What risk exists when disabling ValidationPipe whitelist?
- Slower requests
- Unexpected payload properties
- Broken routing
- Serialization issues
Explanation: Extra properties may pass through.
Question 14: Why should @Res() be avoided in most cases?
- Breaks routing
- Bypasses NestJS response lifecycle
- Disables guards
- Blocks async pipes
Explanation: It bypasses interceptors and filters.
Question 15: Which scenario justifies using @Res({ passthrough: true })?
- Manual routing
- Partial response control with Nest lifecycle
- Faster responses
- Disabling interceptors
Explanation: It allows partial control safely.
Question 16: Why are global interceptors powerful but dangerous?
- Performance cost
- They affect every request
- Hard to test
- They break DI
Explanation: They impact the entire application.
Question 17: What is the main purpose of ExecutionContext?
- Routing
- Access request metadata across contexts
- Error handling
- Dependency injection
Explanation: ExecutionContext abstracts request context.
Question 18: Why is Passport integration implemented via guards?
- Performance
- Authorization fits guard responsibility
- Simpler syntax
- DI limitation
Explanation: Guards handle authorization decisions.
Question 19: What is the biggest JWT authentication pitfall?
- Token size
- Token revocation difficulty
- Encryption cost
- Header parsing
Explanation: JWTs are hard to revoke.
Question 20: Why should refresh tokens be stored securely?
- Performance
- Prevent token theft
- Reduce DB calls
- Improve routing
Explanation: Stolen refresh tokens are dangerous.
Question 21: Which NestJS feature enables CQRS pattern?
- @nestjs/graphql
- @nestjs/cqrs
- @nestjs/typeorm
- @nestjs/config
Explanation: nestjs/cqrs supports CQRS.
Question 22: What is the main benefit of CQRS?
- Faster routing
- Separation of read and write models
- Simpler code
- Reduced memory
Explanation: CQRS separates read/write concerns.
Question 23: Why is EventEmitter not ideal for large-scale systems?
- Slow performance
- Tight coupling and lack of durability
- Memory leaks
- Limited syntax
Explanation: Events are not durable.
Question 24: Which transport is best for high-throughput microservices?
Explanation: NATS is optimized for messaging.
Question 25: Why should DTOs be immutable?
- Performance
- Predictable data flow
- Smaller bundles
- Serialization
Explanation: Immutability prevents side effects.
Question 26: What is the main risk of sharing entities across layers?
- Type conflicts
- Leaky abstractions
- Slow queries
- Circular imports
Explanation: Entities leaking cause tight coupling.
Question 27: Why is Repository pattern preferred in NestJS?
- Less code
- Abstracts data access logic
- Improves routing
- Enhances decorators
Explanation: It decouples persistence logic.
Question 28: What is the purpose of custom providers?
- Override framework behavior
- Control instantiation logic
- Create controllers
- Register routes
Explanation: Custom providers control DI behavior.
Question 29: Which provider type enables dynamic configuration?
- useClass
- useValue
- useFactory
- useExisting
Explanation: Factories allow dynamic creation.
Question 30: Why should heavy logic not run in onModuleInit?
- DI not ready
- Blocks application startup
- Causes memory leaks
- Breaks lifecycle
Explanation: It delays app readiness.
Question 31: What is the biggest challenge in NestJS microservices debugging?
- Syntax errors
- Distributed tracing
- DTO validation
- Transport selection
Explanation: Tracing across services is complex.
Question 32: Why is correlation ID important?
- Security
- Tracking requests across services
- Caching
- Routing
Explanation: It helps trace distributed requests.
Question 33: Which feature enables graceful shutdown?
- enableShutdownHooks()
- app.close()
- process.exit()
- beforeExit()
Explanation: Shutdown hooks allow cleanup.
Question 34: Why is app.enableShutdownHooks() critical in production?
- Performance
- Resource cleanup on termination
- Routing safety
- Async handling
Explanation: It ensures graceful shutdown.
Question 35: Which testing strategy best suits large NestJS apps?
- Only unit tests
- Only e2e tests
- Combination of unit, integration, and e2e
- Manual testing
Explanation: Multiple test layers ensure reliability.
Question 36: Why should e2e tests avoid real external services?
- Speed
- Flakiness and instability
- Cost
- Security
Explanation: External services cause flaky tests.
Question 37: What is the main benefit of using TestContainers?
- Faster tests
- Production-like dependencies
- Simpler setup
- Mock-free tests
Explanation: They provide realistic environments.
Question 38: Why should configuration be validated at startup?
- Performance
- Fail fast on misconfiguration
- Security
- Ease of use
Explanation: Failing fast avoids runtime issues.
Question 39: Which NestJS feature helps version APIs cleanly?
- Guards
- URI versioning
- Interceptors
- Middleware
Explanation: URI or header versioning is supported.
Question 40: What is the main downside of global modules?
- Slower startup
- Hidden dependencies
- Memory usage
- DI conflicts
Explanation: They obscure dependency boundaries.
Question 41: Why is statelessness important for scalability?
- Security
- Horizontal scaling support
- Simpler code
- Better logging
Explanation: Stateless apps scale horizontally.
Question 42: Which architectural mistake most often hurts NestJS performance?
- Using DTOs
- Overusing request-scoped providers
- Using modules
- Using guards
Explanation: Request scope adds overhead.
Question 43: Why should background jobs be separated from HTTP servers?
- Security
- Isolation and scalability
- Code reuse
- Simpler DI
Explanation: Isolation improves reliability.
Question 44: Which NestJS feature supports background job processing?
- @nestjs/schedule
- @nestjs/bull
- @nestjs/queue
- @nestjs/tasks
Explanation: Bull integrates job queues.
Question 45: What is the ultimate architectural goal of NestJS?
- Minimal APIs
- Scalable, maintainable server-side systems
- Rapid prototyping
- Microservices only
Explanation: NestJS targets scalable backend systems.