[{"id":"nestjsbeginner-001","courseId":"nestjs-beginner","domain":"modules-and-di","topic":"Providers as injectable services","prompt":"You write a CatsService class that stores cats in memory. For Nest's container to manage it and inject it into other classes, which decorator must go on the class?","hint":"The decorator goes on the service class itself, not on the module that lists it.","explain":"A container can only manage a class that carries a marker it recognizes. Each decorator tags a different role, such as routing or grouping, so check which role a service needs.","source":"https://docs.nestjs.com/providers.md","interaction":{"type":"single"},"options":[{"id":"a","text":"@Controller('cats')","whyNot":"@Controller() marks a class that handles HTTP routes; it does not make a service manageable by the container."},{"id":"b","text":"@Injectable()","why":"@Injectable() attaches metadata that declares the class can be managed by the Nest IoC container."},{"id":"c","text":"@Module()","whyNot":"@Module() describes a module's providers, controllers and imports; it goes on the module class, not on the service."},{"id":"d","text":"@Global()","whyNot":"@Global() makes a module's exports available everywhere; it does not mark a single class as manageable."}],"answer":"b","explanation":"Nest's IoC container manages classes marked with the Injectable decorator, which attaches metadata declaring that the class can be managed by the container.","concept":"Nest's IoC container manages classes marked with the Injectable decorator, which attaches metadata declaring that the class can be managed by the container.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A service the container injects needs its own marker; the module only lists it."]},{"id":"nestjsbeginner-002","courseId":"nestjs-beginner","domain":"modules-and-di","topic":"Constructor-based injection","prompt":"In this code, AppConfig is an interface. Nest can still inject the provider registered for AppConfig, because the constructor parameter is typed with it.","hint":"Think about what still exists at runtime, and what Nest can use as a lookup key.","explain":"Nest finds a dependency from the type written on a constructor parameter. It can only look up types that still exist when the program runs, so consider which types survive compilation.","source":"https://docs.nestjs.com/providers.md","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"An interface is erased during compilation, so Nest has no class to use as a lookup key and startup fails."},{"id":"b","text":"False","why":"Interfaces are erased at compile time, leaving no runtime token. Register a custom token and inject it explicitly with @Inject()."}],"answer":"b","explanation":"Nest resolves constructor dependencies by the class type emitted at compile time; interfaces are erased, so injecting one requires a custom token with @Inject().","concept":"Nest resolves constructor dependencies by the class type emitted at compile time; interfaces are erased, so injecting one requires a custom token with @Inject().","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"export interface AppConfig {\n  port: number;\n}\n\nexport class ApiService {\n  constructor(private config: AppConfig) {}\n}","takeaways":["Constructor types must be classes; for an interface, register a token and use @Inject()."]},{"id":"nestjsbeginner-003","courseId":"nestjs-beginner","domain":"modules-and-di","topic":"Shutdown hook order","prompt":"A Nest app is shutting down. Put these steps of its shutdown sequence in order, from first to last.","hint":"Ask which stages must finish before the app's outside links are released.","explain":"Stopping an app is a chain of stages, and each stage waits for the one it depends on. Some stages tidy up the app's own parts, while others deal with links to the outside world.","source":"https://docs.nestjs.com/fundamentals/lifecycle-events.md","interaction":{"type":"sort","items":[{"text":"onModuleDestroy() hooks run","why":"Shutdown begins with these hooks, and Nest waits for each returned promise to settle before it moves on."},{"text":"beforeApplicationShutdown() hooks run","why":"They run once every onModuleDestroy() handler has settled, and before existing connections are closed."},{"text":"Existing connections are closed","why":"Nest closes them once the hooks before this point have settled."},{"text":"onApplicationShutdown() hooks run","why":"They run after connections have closed, as the last step of app.close()."}]},"options":[],"answer":"o:abcd","explanation":"Nest runs onModuleDestroy(), then beforeApplicationShutdown(), then closes existing connections, and finally onApplicationShutdown(); shutdown hooks run only when enabled.","concept":"Nest runs onModuleDestroy(), then beforeApplicationShutdown(), then closes existing connections, and finally onApplicationShutdown(); shutdown hooks run only when enabled.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Shutdown hooks run only after app.enableShutdownHooks() is called during bootstrap."]},{"id":"nestjsbeginner-004","courseId":"nestjs-beginner","domain":"modules-and-di","topic":"Feature modules","prompt":"CatsModule provides CatsService and exports it. Three other modules each import CatsModule. How many CatsService objects does the application create?","hint":"Think about how many times a module is set up when several other modules import it.","explain":"Every module is a reusable unit: once it has been created, any module can import it rather than build its own. Consider what that means for the providers a module exports.","source":"https://docs.nestjs.com/modules.md","interaction":{"type":"single"},"options":[{"id":"a","text":"One per importing module, so three in total","whyNot":"Importing a module does not copy it; Nest reuses the one module and the provider it exports."},{"id":"b","text":"One, shared by all three importing modules","why":"Modules are singletons, so the exported CatsService is created once and every importing module reuses it."},{"id":"c","text":"One per injection site, so every constructor gets its own","whyNot":"Nest creates a default-scoped provider once and gives the same instance to every consumer."},{"id":"d","text":"None; each importing module must list CatsService itself","whyNot":"Importing CatsModule already gives access to CatsService; listing it again would only create a separate copy."}],"answer":"b","explanation":"Modules are singletons by default. A provider a module exports is one instance that every module importing that module reuses.","concept":"Modules are singletons by default. A provider a module exports is one instance that every module importing that module reuses.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Share a provider by exporting it and importing its module; don't re-register it elsewhere."]},{"id":"nestjsbeginner-005","courseId":"nestjs-beginner","domain":"modules-and-di","topic":"Module imports and exports","prompt":"CatsService is defined in CatsModule, and module B needs to use it. Which TWO changes let B use the same CatsService instance that CatsModule creates?","hint":"Two modules are involved: the one that owns the provider and the one that wants it.","explain":"A module keeps its providers to itself unless it chooses to hand them out. Consider which side has to declare something, and which side has to ask for it.","source":"https://docs.nestjs.com/modules.md","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Add CatsService to the providers array of module B","whyNot":"That registers a second provider, so B gets its own separate instance instead of the shared one."},{"id":"b","text":"Add CatsService to the exports array of CatsModule","why":"Exporting makes the provider available to the modules that import CatsModule."},{"id":"c","text":"Inject the CatsModule class into the constructor of module B","whyNot":"Module classes cannot be injected as providers, because of circular dependencies."},{"id":"d","text":"Add CatsModule to the imports array of module B","why":"Importing the module gives B access to the providers it exports, and they keep the same instance."},{"id":"e","text":"Add CatsService to the controllers array of CatsModule","whyNot":"The controllers array holds route handlers, so a service listed there is not shared with other modules."},{"id":"f","text":"Add CatsService to the imports array of CatsModule","whyNot":"The imports array takes modules, not providers, so this does not expose CatsService to anyone."}],"answer":"bd","explanation":"A module's providers are private unless the module lists them in exports; a consumer reaches them by importing the module that exports them.","concept":"A module's providers are private unless the module lists them in exports; a consumer reaches them by importing the module that exports them.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Share a provider by exporting it, and let other modules reach it through their imports."]}]