Bird
Raised Fist0
NestJSframework~20 mins

Service creation in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this NestJS service method call?
Consider this NestJS service method that returns a greeting message. What will be the output when getGreeting() is called?
NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class GreetingService {
  getGreeting(): string {
    return `Hello, NestJS!`;
  }
}

const service = new GreetingService();
console.log(service.getGreeting());
AHello, NestJS!
Bundefined
CError: Cannot read property 'getGreeting' of undefined
DHello, World!
Attempts:
2 left
💡 Hint
Look at the return statement inside the getGreeting method.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a NestJS service with dependency injection?
You want to create a NestJS service OrderService that depends on PaymentService. Which option correctly injects PaymentService into OrderService?
A
import { Injectable } from '@nestjs/common';

export class OrderService {
  constructor(private paymentService: PaymentService) {}
}
B
import { Injectable } from '@nestjs/common';

@Injectable()
export class OrderService {
  paymentService: PaymentService;
  constructor() {
    this.paymentService = new PaymentService();
  }
}
C
import { Injectable } from '@nestjs/common';

@Injectable()
export class OrderService {
  constructor(private paymentService: PaymentService) {}
}
D
import { Injectable } from '@nestjs/common';

@Injectable()
export class OrderService {
  constructor(paymentService: PaymentService) {
    this.paymentService = paymentService;
  }
}
Attempts:
2 left
💡 Hint
Look for the correct use of @Injectable and constructor parameter visibility.
🔧 Debug
advanced
2:00remaining
Why does this NestJS service fail to inject the dependency?
Given the following code, why will NestJS fail to inject UserRepository into UserService?
NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class UserService {
  constructor(private userRepository: UserRepository) {}
}

@Injectable()
export class UserRepository {}
AUserService is missing the @Controller decorator.
BThe constructor parameter should be public, not private.
CUserRepository must be imported from a module, not declared in the same file.
DUserRepository is not decorated with @Injectable(), so NestJS cannot inject it.
Attempts:
2 left
💡 Hint
Check if all injectable classes have the @Injectable decorator.
❓ state_output
advanced
2:00remaining
What is the value of count after calling increment() twice?
Consider this NestJS service that tracks a count. What will be the value of count after calling increment() two times?
NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class CounterService {
  private count = 0;

  increment() {
    this.count++;
  }

  getCount() {
    return this.count;
  }
}

const service = new CounterService();
service.increment();
service.increment();
const result = service.getCount();
A1
B2
C0
Dundefined
Attempts:
2 left
💡 Hint
Each call to increment increases count by one.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of services in NestJS?
In NestJS, what is the primary purpose of a service?
ATo encapsulate business logic and provide reusable functionality.
BTo style components and manage UI layout.
CTo manage database connections directly without abstraction.
DTo handle HTTP requests and define routes.
Attempts:
2 left
💡 Hint
Think about where the core logic of your app should live.

Practice

(1/5)
1.

What is the main purpose of creating a service in a NestJS application?

easy
A. To handle HTTP requests directly
B. To style the user interface
C. To define database schemas
D. To hold the business logic separate from controllers

Solution

  1. Step 1: Understand the role of services in NestJS

    Services are designed to contain business logic, keeping controllers clean and focused on handling requests.
  2. Step 2: Differentiate services from controllers and other parts

    Controllers handle HTTP requests, schemas define data structure, and styling is unrelated to services.
  3. Final Answer:

    To hold the business logic separate from controllers -> Option D
  4. Quick Check:

    Service purpose = business logic separation [OK]
Hint: Services = business logic holders, not request handlers [OK]
Common Mistakes:
  • Confusing services with controllers
  • Thinking services handle HTTP requests
  • Assuming services define database schemas
2.

Which decorator is used to make a class injectable as a service in NestJS?

easy
A. @Injectable()
B. @Module()
C. @Controller()
D. @Service()

Solution

  1. Step 1: Identify the decorator for service injection

    In NestJS, @Injectable() marks a class as a provider that can be injected.
  2. Step 2: Differentiate from other decorators

    @Controller() is for controllers, @Module() for modules, and @Service() does not exist in NestJS.
  3. Final Answer:

    @Injectable() -> Option A
  4. Quick Check:

    Service decorator = @Injectable() [OK]
Hint: Remember: Injectable means ready to inject [OK]
Common Mistakes:
  • Using @Controller() instead of @Injectable()
  • Confusing @Module() with service decorator
  • Assuming @Service() exists in NestJS
3.

Given the following service code, what will be the output when getHello() is called?

import { Injectable } from '@nestjs/common';

@Injectable()
export class HelloService {
  getHello(): string {
    return 'Hello, NestJS!';
  }
}
medium
A. "Hello, NestJS!"
B. undefined
C. Error: Method not found
D. "Hello World!"

Solution

  1. Step 1: Analyze the getHello method

    The method returns the string 'Hello, NestJS!'.
  2. Step 2: Determine the output of calling getHello()

    Calling getHello() returns exactly the string defined, no errors or undefined.
  3. Final Answer:

    "Hello, NestJS!" -> Option A
  4. Quick Check:

    Method return = "Hello, NestJS!" [OK]
Hint: Read method return value carefully [OK]
Common Mistakes:
  • Assuming method returns undefined
  • Confusing output with error messages
  • Mixing up string content
4.

Identify the error in this service code snippet:

import { Injectable } from '@nestjs/common';

@Injectable()
export class MathService {
  add(a: number, b: number) {
    return a + b
  }
}
medium
A. Missing parentheses in method declaration
B. Missing return type annotation for add method
C. No error, code is correct
D. Missing semicolon after return statement

Solution

  1. Step 1: Check syntax correctness

    The method add correctly takes two numbers and returns their sum. Semicolons are optional in TypeScript.
  2. Step 2: Verify method declaration and return type

    Return type annotation is optional; TypeScript infers it. Parentheses are present and correct.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    Code syntax = correct [OK]
Hint: Semicolons optional; return types inferred [OK]
Common Mistakes:
  • Thinking semicolons are mandatory
  • Expecting explicit return type always
  • Misreading method syntax
5.

You want to create a service that provides a list of users and inject it into a controller. Which of the following is the correct way to create and inject the service?

1. Create service with @Injectable()
2. Define a method getUsers() returning an array
3. Inject service in controller constructor
4. Use service method inside controller method
hard
A. Create service without @Injectable(), inject with @Inject() decorator
B. Create service with @Injectable(), inject via constructor parameter
C. Create service with @Controller(), inject via property decorator
D. Create service with @Module(), inject manually inside methods

Solution

  1. Step 1: Create service with @Injectable()

    This decorator makes the service injectable and reusable.
  2. Step 2: Inject service via constructor parameter in controller

    Constructor injection is the NestJS standard for dependency injection.
  3. Step 3: Use service methods inside controller methods

    This keeps code modular and clean.
  4. Final Answer:

    Create service with @Injectable(), inject via constructor parameter -> Option B
  5. Quick Check:

    Service injection = @Injectable() + constructor [OK]
Hint: Use @Injectable() and constructor injection [OK]
Common Mistakes:
  • Omitting @Injectable() on service
  • Using @Controller() on service
  • Injecting service incorrectly with property decorators
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