0
0
NestJSframework~20 mins

Service creation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 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.