Bird
Raised Fist0
NestJSframework~20 mins

Feature modules in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS Feature Modules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when importing a feature module with providers?

Consider a NestJS feature module that provides a service. What happens when this module is imported into the root module?

NestJS
import { Module, Injectable } from '@nestjs/common';

@Injectable()
export class GreetingService {
  getGreeting() {
    return 'Hello from Feature Module!';
  }
}

@Module({
  providers: [GreetingService],
  exports: [GreetingService],
})
export class FeatureModule {}

@Module({
  imports: [FeatureModule],
})
export class AppModule {}

// In a controller inside AppModule
constructor(private greetingService: GreetingService) {}

getHello() {
  return this.greetingService.getGreeting();
}
AThe GreetingService is not available in AppModule and will cause a runtime error.
BThe GreetingService is only available inside FeatureModule and cannot be injected elsewhere.
CThe FeatureModule must be declared in controllers to use GreetingService.
DThe AppModule can use GreetingService and get 'Hello from Feature Module!' as output.
Attempts:
2 left
💡 Hint

Think about how providers are shared when exported from a module.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a feature module with a controller and service?

Identify the correct NestJS feature module syntax that includes a controller and a service.

A
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: (MyController),
  providers: (MyService),
})
export class MyFeatureModule {}
B
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: MyController,
  providers: MyService,
})
export class MyFeatureModule {}
C
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controllers: [MyController],
  providers: [MyService],
})
export class MyFeatureModule {}
D
import { Module } from '@nestjs/common';
import { MyController } from './my.controller';
import { MyService } from './my.service';

@Module({
  controller: [MyController],
  provider: [MyService],
})
export class MyFeatureModule {}
Attempts:
2 left
💡 Hint

Check the property names and array syntax in the @Module decorator.

🔧 Debug
advanced
2:00remaining
Why does injecting a service from a feature module cause a runtime error?

Given a feature module that provides a service but the service injection fails with 'Nest can't resolve dependencies' error, what is the likely cause?

NestJS
import { Module, Injectable } from '@nestjs/common';

@Injectable()
export class DataService {
  getData() {
    return 'data';
  }
}

@Module({
  providers: [DataService],
  // exports: [DataService], // This line is missing
})
export class DataModule {}

@Module({
  imports: [DataModule],
})
export class AppModule {}

// In a controller inside AppModule
constructor(private dataService: DataService) {}
ADataService must be declared in AppModule providers to be injectable.
BDataService is not exported from DataModule, so it is not available for injection in AppModule.
CDataService should be marked as @Global() to be injectable.
DDataService requires a factory provider to be injectable.
Attempts:
2 left
💡 Hint

Check if the service is exported from the module that provides it.

❓ state_output
advanced
2:00remaining
What is the value of a service property after importing multiple feature modules?

Consider two feature modules each providing a service with a counter property initialized to 0. If both modules are imported into AppModule and the services are injected separately, what are the counter values after incrementing each once?

NestJS
import { Module, Injectable } from '@nestjs/common';

@Injectable()
export class CounterService {
  counter = 0;
  increment() {
    this.counter++;
  }
}

@Module({
  providers: [CounterService],
  exports: [CounterService],
})
export class ModuleA {}

@Module({
  providers: [CounterService],
  exports: [CounterService],
})
export class ModuleB {}

@Module({
  imports: [ModuleA, ModuleB],
})
export class AppModule {}

// In a controller
constructor(
  private counterA: CounterService,
  private counterB: CounterService
) {}

someMethod() {
  this.counterA.increment();
  this.counterB.increment();
  return { a: this.counterA.counter, b: this.counterB.counter };
}
A{ a: 1, b: 1 } because each module provides its own instance of CounterService.
B{ a: 2, b: 2 } because CounterService is singleton across modules.
C{ a: 1, b: 0 } because only counterA increments.
D{ a: 0, b: 0 } because services are not instantiated.
Attempts:
2 left
💡 Hint

Think about how NestJS creates service instances per module.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of feature modules in NestJS?

Choose the most accurate description of feature modules in NestJS.

AFeature modules organize related components and services, encapsulating functionality to promote modularity and reusability.
BFeature modules are only used to lazy-load parts of the application and cannot export providers.
CFeature modules replace the root module and must contain all controllers and services.
DFeature modules automatically share all their providers globally without explicit exports.
Attempts:
2 left
💡 Hint

Consider how NestJS encourages modular design.

Practice

(1/5)
1. What is the main purpose of a feature module in NestJS?
easy
A. To manage HTTP requests without controllers
B. To group related controllers and services for better organization
C. To replace the main application module
D. To handle database connections directly

Solution

  1. Step 1: Understand the role of feature modules

    Feature modules group related parts of the app like controllers and services to keep code organized.
  2. Step 2: Compare options with this role

    Only To group related controllers and services for better organization describes grouping related controllers and services, which matches the purpose.
  3. Final Answer:

    To group related controllers and services for better organization -> Option B
  4. Quick Check:

    Feature modules organize code = D [OK]
Hint: Feature modules group related parts of your app [OK]
Common Mistakes:
  • Thinking feature modules handle database connections directly
  • Confusing feature modules with the main app module
  • Assuming feature modules replace controllers
2. Which of the following is the correct way to define a feature module in NestJS?
easy
A. @Controller({}) export class FeatureModule {}
B. @Injectable() export class FeatureModule {}
C. @Module({ controllers: [], providers: [] }) export class FeatureModule {}
D. export class FeatureModule {}

Solution

  1. Step 1: Recall the decorator for modules

    Feature modules use the @Module decorator with metadata like controllers and providers.
  2. Step 2: Match options with correct decorator

    Only @Module({ controllers: [], providers: [] }) export class FeatureModule {} uses @Module with controllers and providers arrays, which is correct syntax.
  3. Final Answer:

    @Module({ controllers: [], providers: [] }) export class FeatureModule {} -> Option C
  4. Quick Check:

    @Module decorator defines modules = B [OK]
Hint: Use @Module decorator with controllers and providers [OK]
Common Mistakes:
  • Using @Controller or @Injectable instead of @Module
  • Forgetting to add the decorator
  • Defining empty classes without decorators
3. Given this feature module code, what will be the output when the getHello() method is called from HelloController?
@Module({
  controllers: [HelloController],
  providers: [HelloService],
})
export class HelloModule {}

@Injectable()
export class HelloService {
  getHello() {
    return 'Hello from service!';
  }
}

@Controller()
export class HelloController {
  constructor(private readonly helloService: HelloService) {}

  @Get()
  getHello() {
    return this.helloService.getHello();
  }
}
medium
A. undefined
B. "Hello from controller!"
C. Runtime error due to missing provider
D. "Hello from service!"

Solution

  1. Step 1: Understand dependency injection in the module

    The HelloService is provided in the module and injected into HelloController.
  2. Step 2: Trace the method call

    The controller's getHello calls helloService.getHello(), which returns 'Hello from service!'.
  3. Final Answer:

    "Hello from service!" -> Option D
  4. Quick Check:

    Service method returns string = C [OK]
Hint: Controller calls service method returning string [OK]
Common Mistakes:
  • Assuming controller returns its own string
  • Thinking provider is missing causing error
  • Confusing method names or return values
4. Identify the error in this feature module definition:
@Module({
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

@Injectable()
export class UserService {}

@Controller()
export class UserController {
  constructor(userService: UserService) {}
}
medium
A. Missing access modifier 'private' in constructor injection
B. UserService is not decorated with @Injectable()
C. UserController is missing @Controller() decorator
D. UserService is not listed in providers array

Solution

  1. Step 1: Check constructor injection syntax

    In NestJS, injected dependencies must have an access modifier like private or public to create a class property.
  2. Step 2: Identify the missing modifier

    The constructor parameter 'userService' lacks 'private' or 'public', so it won't be assigned automatically.
  3. Final Answer:

    Missing access modifier 'private' in constructor injection -> Option A
  4. Quick Check:

    Constructor injection needs access modifier = A [OK]
Hint: Constructor injection needs private/public keyword [OK]
Common Mistakes:
  • Forgetting private/public in constructor parameters
  • Assuming @Injectable is missing when it is present
  • Confusing decorator placement
5. You want to create a feature module that imports another feature module to reuse its services. Which of the following is the correct way to import AuthModule into UserModule?
hard
A. @Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {}
B. @Module({ controllers: [UserController], providers: [UserService] }) export class UserModule { constructor(private authModule: AuthModule) {} }
C. @Module({ exports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {}
D. @Module({ imports: [], controllers: [UserController], providers: [UserService] }) export class UserModule {}

Solution

  1. Step 1: Understand module imports in NestJS

    To reuse services from another module, you must import that module in the imports array.
  2. Step 2: Identify correct import usage

    @Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} correctly adds AuthModule to imports, allowing UserModule to use its exported providers.
  3. Final Answer:

    @Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} -> Option A
  4. Quick Check:

    Import modules via imports array = A [OK]
Hint: Use imports array to include other feature modules [OK]
Common Mistakes:
  • Trying to inject modules via constructor instead of imports
  • Using exports instead of imports to include modules
  • Leaving imports array empty when reusing modules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard