NestJS Quiz ( Intermediate ) - All Questions
This intermediate NestJS quiz is designed for developers who know the basics and want to test real-world usage of NestJS. It covers dependency injection scopes, modules communication, guards, interceptors, pipes, middleware flow, validation, authentication basics, and common interview scenarios.
Question 1: What is the main responsibility of AppModule in NestJS?
- Handle routing
- Bootstrap the application
- Group feature modules
- Configure middleware
Explanation: AppModule groups and imports all feature modules.
Question 2: How does NestJS resolve dependencies by default?
- Factory pattern
- Singleton instances
- Prototype instances
- Manual injection
Explanation: NestJS providers are singleton by default.
Question 3: Which provider scope creates a new instance per request?
- DEFAULT
- STATIC
- REQUEST
- TRANSIENT
Explanation: REQUEST scope creates a provider per request.
Question 4: Which provider scope creates a new instance every injection?
- DEFAULT
- REQUEST
- GLOBAL
- TRANSIENT
Explanation: TRANSIENT creates a new instance on every injection.
Question 5: How can a provider be shared across modules?
- Declare it again
- Export it from a module
- Use global variables
- Import main.ts
Explanation: Exporting a provider allows reuse across modules.
Question 6: What is the purpose of @Global() decorator?
- Create global routes
- Make a module globally available
- Enable middleware globally
- Register controllers
Explanation: @Global makes providers available across the app.
Question 7: Which NestJS feature is executed before route handlers?
- Interceptors
- Guards
- Pipes
- Exception filters
Explanation: Guards run before route handlers for authorization.
Question 8: What is the primary role of Guards?
- Data validation
- Authorization logic
- Response mapping
- Error handling
Explanation: Guards determine whether a request is allowed.
Question 9: Which interface must a Guard implement?
- CanActivate
- NestGuard
- AuthGuard
- Activate
Explanation: Guards implement the CanActivate interface.
Question 10: Which NestJS feature transforms incoming request data?
- Interceptors
- Guards
- Pipes
- Middleware
Explanation: Pipes transform and validate input data.
Question 11: Which pipe converts a string parameter to number?
- ValidationPipe
- ParseIntPipe
- DefaultValuePipe
- TransformPipe
Explanation: ParseIntPipe converts strings to numbers.
Question 12: What does ValidationPipe rely on?
Explanation: ValidationPipe uses class-validator.
Question 13: Which decorator enables global pipes?
- app.usePipe()
- app.useGlobalPipes()
- @UsePipes()
- @GlobalPipe()
Explanation: useGlobalPipes applies pipes globally.
Question 14: Which feature wraps method execution for extra logic?
- Middleware
- Guards
- Interceptors
- Filters
Explanation: Interceptors wrap method execution.
Question 15: Which common use case fits Interceptors?
- Authentication
- Authorization
- Logging and response mapping
- Input validation
Explanation: Interceptors are used for logging and transformation.
Question 16: What is the execution order in NestJS request lifecycle?
- Middleware → Guards → Pipes → Interceptors → Controller
- Guards → Middleware → Pipes → Controller
- Pipes → Guards → Interceptors → Middleware
- Interceptors → Guards → Pipes → Controller
Explanation: This is the correct execution sequence.
Question 17: Which feature catches and formats thrown errors?
- Guards
- Interceptors
- Exception filters
- Middleware
Explanation: Exception filters handle errors.
Question 18: Which base class handles HTTP exceptions?
- BaseException
- HttpError
- HttpException
- NestException
Explanation: HttpException is used for HTTP errors.
Question 19: Which decorator applies exception filters?
- @UseFilters()
- @Catch()
- @Filter()
- @HandleError()
Explanation: @UseFilters applies exception filters.
Question 20: What is middleware mainly used for?
- Authorization
- Request preprocessing
- Response mapping
- Error handling
Explanation: Middleware runs before route handling.
Question 21: Which middleware type is NOT supported by NestJS?
- Class-based
- Function-based
- Third-party
- Decorator-based
Explanation: NestJS does not support decorator-based middleware.
Question 22: Which decorator injects the request object?
- @Req()
- @Request()
- @Body()
- @Ctx()
Explanation: @Req accesses the request object.
Question 23: Which decorator injects response object?
- @Res()
- @Response()
- @Reply()
- @Out()
Explanation: @Res injects the response object.
Question 24: What happens when using @Res() without passthrough?
- NestJS auto-handles response
- You must manually send response
- Interceptor still works
- Pipe validation fails
Explanation: Manual response handling is required.
Question 25: Which feature supports authentication strategies?
Explanation: NestJS integrates with Passport.
Question 26: Which Passport strategy is commonly used for JWT?
- LocalStrategy
- JwtStrategy
- OAuthStrategy
- SessionStrategy
Explanation: JwtStrategy handles JWT authentication.
Question 27: Which decorator extracts authenticated user?
- @Req()
- @User()
- @Auth()
- Custom decorator
Explanation: Custom decorators extract user data.
Question 28: Which module registers environment variables?
- ConfigModule
- EnvModule
- DotenvModule
- SystemModule
Explanation: ConfigModule manages environment variables.
Question 29: Which method enables global configuration?
- ConfigModule.setup()
- ConfigModule.forRoot()
- ConfigModule.init()
- ConfigModule.load()
Explanation: forRoot loads config globally.
Question 30: Which decorator marks a route as public using custom guards?
- @Open()
- @Public()
- @Allow()
- Custom decorator
Explanation: Public routes require a custom decorator.
Question 31: Which NestJS feature enables modular scalability?
- Providers
- Modules
- Controllers
- Pipes
Explanation: Modules enable scalable architecture.
Question 32: What does forwardRef() solve?
- Lazy loading
- Circular dependency issues
- Async imports
- Route conflicts
Explanation: forwardRef resolves circular dependencies.
Question 33: Which scenario requires forwardRef?
- Module-to-module reference
- Circular module dependency
- Service injection
- Middleware injection
Explanation: Used when modules depend on each other.
Question 34: Which lifecycle hook runs on module initialization?
- onInit()
- beforeInit()
- onModuleInit()
- afterInit()
Explanation: onModuleInit runs after module creation.
Question 35: Which lifecycle hook runs before app shutdown?
- beforeExit()
- onDestroy()
- beforeShutdown()
- enableShutdownHooks
Explanation: beforeShutdown runs during shutdown.
Question 36: Which feature supports GraphQL in NestJS?
- @nestjs/graphql
- apollo-server
- graphql-js
- TypeGraphQL
Explanation: @nestjs/graphql integrates GraphQL.
Question 37: Which transport layer supports microservices?
- HTTP only
- TCP, Redis, NATS
- WebSocket only
- gRPC only
Explanation: NestJS supports multiple transports.
Question 38: Which decorator defines message patterns?
- @Event()
- @MessagePattern()
- @Subscribe()
- @Handle()
Explanation: @MessagePattern handles microservice messages.
Question 39: Which feature improves testability in NestJS?
- Decorators
- Dependency Injection
- Controllers
- Modules
Explanation: DI enables easy mocking in tests.
Question 40: Which testing utility creates a testing module?
- NestFactory
- Test.createTestingModule()
- TestingModule
- NestTest
Explanation: createTestingModule is used in tests.
Question 41: Which file configures build options?
- package.json
- nest-cli.json
- tsconfig.json
- main.ts
Explanation: nest-cli.json controls build config.
Question 42: Which command builds NestJS for production?
- nest serve
- npm run build
- nest start --prod
- npm start
Explanation: npm run build creates production build.
Question 43: Which feature supports REST and GraphQL together?
- Controllers
- Resolvers
- Hybrid application
- Middleware
Explanation: NestJS supports hybrid apps.
Question 44: Which practice improves NestJS performance?
- Heavy middleware usage
- Stateless services
- Request-scoped providers everywhere
- Manual DI
Explanation: Stateless services scale better.
Question 45: What is the main architectural advantage of NestJS?
- Minimal setup
- Opinionated and scalable structure
- Frontend rendering
- Database abstraction
Explanation: NestJS enforces scalable architecture.
Question 46: What is the ultimate goal of NestJS?
- Static APIs
- Scalable server-side applications
- CLI tooling
- Microservices only
Explanation: NestJS is built for scalable backend systems.