[{"id":"nestjsintermediate-001","courseId":"nestjs-intermediate","domain":"modules-and-di","topic":"useValue constant providers","prompt":"Nest must supply a constructor parameter typed as CatsService, a provider registered in the same module. Put these steps of its resolution in order.","hint":"Think about what the container must know before it can hand anything out.","explain":"Supplying a consumer's needs works as a chain: each decision rests on an earlier one, and whether an existing copy can be kept depends on knowing exactly which thing is wanted.","source":"https://docs.nestjs.com/fundamentals/custom-providers.md","interaction":{"type":"sort","items":[{"text":"Nest scans the CatsController constructor and lists its dependencies.","why":"The container must know what a class asks for before it can supply anything to it."},{"text":"Nest looks up the CatsService token among the module's registrations.","why":"A token is only a key; it has to be matched to its registration before anything can be built."},{"text":"The lookup returns the CatsService class that was registered under that token.","why":"Only the matched registration says which class to build."},{"text":"Nest creates a CatsService and caches it, or returns the cached one.","why":"Under the default singleton scope, one instance is built and then returned to every later consumer."}]},"options":[],"answer":"o:abcd","explanation":"Nest resolves a constructor dependency by finding it, looking up its token to get the registered class, then creating or reusing the instance under its scope.","concept":"Nest resolves a constructor dependency by finding it, looking up its token to get the registered class, then creating or reusing the instance under its scope.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A token is only a lookup key; the registration decides which class gets built."]},{"id":"nestjsintermediate-002","courseId":"nestjs-intermediate","domain":"modules-and-di","topic":"useClass implementation switching","prompt":"In a module, a useClass provider can choose its implementation from a condition, so the same token resolves to one class in development and another in production.","hint":"Think about what the useClass entry is allowed to contain when Nest reads it.","explain":"A token is a lookup key, and its registration decides what consumers receive. They ask for the token by name, never naming a class, so the registration alone controls the outcome.","source":"https://docs.nestjs.com/fundamentals/custom-providers.md","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"The useClass value is ordinary code, so an expression such as a check on NODE_ENV can pick the class at startup."},{"id":"b","text":"False","whyNot":"The useClass entry can hold any expression that yields a class, so a condition on the environment is allowed."}],"answer":"a","explanation":"useClass lets any expression choose the class behind a token, so the environment can decide which implementation consumers receive.","concept":"useClass lets any expression choose the class behind a token, so the environment can decide which implementation consumers receive.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["useClass can switch implementations per environment without changing any consumer."]},{"id":"nestjsintermediate-003","courseId":"nestjs-intermediate","domain":"modules-and-di","topic":"useFactory with inject array","prompt":"A useFactory provider lists inject: [ConfigService, { token: 'CACHE', optional: true }]. Which TWO statements about how Nest passes these to the factory are correct?","hint":"Look at how the two lists line up, and at what an entry marked optional may become.","explain":"A value can be built by running a function, and that function may need other pieces of the app as its inputs. The inputs are listed in a fixed sequence, and a listed input is allowed to come up empty.","source":"https://docs.nestjs.com/fundamentals/custom-providers.md","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Nest passes each resolved instance to the factory as an argument, in the same order as the inject entries.","why":"The inject list is correlated with the factory's parameters by position, so the first entry fills the first argument."},{"id":"b","text":"An inject entry can be written as { token, optional: true }, so the factory receives undefined when nothing is registered under that token.","why":"Marking an entry optional lets it resolve to undefined instead of supplying an instance."},{"id":"c","text":"Nest matches each inject entry to a factory parameter by the parameter's name, so the names must agree.","whyNot":"Matching is positional; the parameter names inside the factory can be anything."},{"id":"d","text":"A factory with no parameters must still declare an inject array, or Nest refuses to start.","whyNot":"inject is optional, and a simple factory that depends on nothing can leave it out."},{"id":"e","text":"A string token in the inject array is rejected; only class tokens can be listed there.","whyNot":"Tokens can be strings or symbols, and the documented example injects a string token."},{"id":"f","text":"An optional entry with no matching registration stops the factory from running, so bootstrap fails.","whyNot":"An optional entry resolves to undefined, and the factory still runs with that argument empty."}],"answer":"ab","explanation":"The inject list maps by position onto the factory's arguments, and an entry marked optional may resolve to undefined.","concept":"The inject list maps by position onto the factory's arguments, and an entry marked optional may resolve to undefined.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Factory arguments come from inject by position; optional entries may arrive as undefined."]},{"id":"nestjsintermediate-004","courseId":"nestjs-intermediate","domain":"modules-and-di","topic":"useExisting alias providers","prompt":"A legacy module injects a string token 'AUDIT'. The app's real provider is the class AuditService, and both injections must share one instance. Which provider entry achieves this?","hint":"Ask whether the new token should build its own object or point at one that already exists.","explain":"One token can be made to point at a provider that is already registered under another token. When both tokens use the default lifetime, every consumer of either one receives the same object.","source":"https://docs.nestjs.com/fundamentals/custom-providers.md","interaction":{"type":"single"},"options":[{"id":"a","text":"{ provide: 'AUDIT', useExisting: AuditService }","why":"useExisting makes 'AUDIT' an alias for the AuditService provider, so both tokens resolve to one instance."},{"id":"b","text":"{ provide: 'AUDIT', useClass: AuditService }","whyNot":"This creates a separate provider that builds its own AuditService, so the two injections get different instances."},{"id":"c","text":"{ provide: AuditService, useExisting: 'AUDIT' }","whyNot":"The direction is reversed: the alias must be the new token and useExisting names the original provider."},{"id":"d","text":"{ provide: 'AUDIT', useValue: AuditService }","whyNot":"useValue hands out the class itself as a constant, so consumers get the constructor, not an AuditService instance."}],"answer":"a","explanation":"useExisting declares an alias: a second token that resolves to an already registered provider, so under singleton scope both tokens yield the same instance.","concept":"useExisting declares an alias: a second token that resolves to an already registered provider, so under singleton scope both tokens yield the same instance.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Use useExisting to expose one provider under a second token without duplicating it."]},{"id":"nestjsintermediate-005","courseId":"nestjs-intermediate","domain":"modules-and-di","topic":"String and symbol injection tokens","prompt":"You want a service to depend on the TypeScript interface Notifier, and the interface is not a class. Which setup correctly registers a provider for it and injects it?","hint":"Ask which parts of the code still exist when the program runs.","explain":"Types are erased at compile time, so a running program keeps no trace of an interface. A DI token must exist at runtime, and an identity no other code shares avoids accidental clashes.","source":"https://docs.nestjs.com/fundamentals/custom-providers.md","interaction":{"type":"single"},"options":[{"id":"a","text":"Export a Symbol constant NOTIFIER, register it as the provide token, and inject it with @Inject(NOTIFIER).","why":"A runtime symbol can serve as the token, and the same exported symbol is reused at registration and injection."},{"id":"b","text":"Register { provide: Notifier, useClass: EmailNotifier } and inject it by the Notifier type alone.","whyNot":"Interfaces are erased at compile time, so there is no runtime Notifier to use as a token."},{"id":"c","text":"Register the plain string 'Notifier' and inject it by the parameter's type, with no @Inject() decorator.","whyNot":"Constructor injection by type needs a class token; a string token must be named explicitly with @Inject()."},{"id":"d","text":"Call Symbol('NOTIFIER') in two different files and rely on matching descriptions to connect them.","whyNot":"Each Symbol call is unique, so only the one exported symbol connects registration and injection."}],"answer":"a","explanation":"Interfaces are erased at compile time, so a provider for one needs a runtime token; a Symbol is unique, and the same exported one must be reused for registration and injection.","concept":"Interfaces are erased at compile time, so a provider for one needs a runtime token; a Symbol is unique, and the same exported one must be reused for registration and injection.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["An interface needs a runtime token: a string or Symbol used with @Inject()."]}]