0
0
NestJSframework~20 mins

First NestJS application - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Novice Master
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 simple NestJS controller?
Consider this NestJS controller code. What will be the HTTP response body when a GET request is made to the root path '/'?
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  getHello(): string {
    return 'Hello, NestJS!';
  }
}
A"Hello, NestJS!"
B"{ message: 'Hello, NestJS!' }"
CAn empty response with status 200
DA 404 Not Found error
Attempts:
2 left
💡 Hint
Look at the return value of the getHello() method and how NestJS sends it as the response body.
state_output
intermediate
2:00remaining
What is the value of 'count' after three POST requests?
Given this NestJS service and controller, what will be the value of 'count' after three POST requests to '/increment'?
NestJS
import { Injectable, Controller, Post } from '@nestjs/common';

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

  increment() {
    this.count++;
    return this.count;
  }

  getCount() {
    return this.count;
  }
}

@Controller()
export class CounterController {
  constructor(private readonly counterService: CounterService) {}

  @Post('increment')
  increment() {
    return this.counterService.increment();
  }
}
AAn error occurs because 'count' is private
B3
C1
D0
Attempts:
2 left
💡 Hint
The service instance is shared and 'count' is incremented each time increment() is called.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a NestJS module with a controller and service?
Identify the correct NestJS module definition that properly imports the controller and service.
A
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
B
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [AppController],
  providers: [AppService],
})
export class AppModule {}
C
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppService],
  providers: [AppController],
})
export class AppModule {}
D
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: AppController,
  providers: AppService,
})
export class AppModule {}
Attempts:
2 left
💡 Hint
Controllers go in 'controllers' array, services in 'providers' array, both as arrays.
🔧 Debug
advanced
2:00remaining
Why does this NestJS app throw a runtime error on startup?
Given this code snippet, why does the NestJS application fail to start?
NestJS
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [],
})
export class AppModule {}
AAppController is missing from controllers array
BThe import statements are incorrect
CThe @Module decorator is missing parentheses
DAppService is not listed in providers, so dependency injection fails if AppController depends on it
Attempts:
2 left
💡 Hint
Check if all dependencies used by controllers are provided in the module.
🧠 Conceptual
expert
3:00remaining
What happens if you mark a NestJS service as @Injectable({ scope: Scope.REQUEST })?
In NestJS, what is the effect of setting a service's scope to REQUEST using @Injectable({ scope: Scope.REQUEST })?
NestJS
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class RequestScopedService {
  private readonly id = Math.random();

  getId() {
    return this.id;
  }
}
AThe service is not injectable and causes a compile-time error
BThe service behaves as a singleton shared across all requests
CA new instance of the service is created for each incoming HTTP request
DThe service instance is created once per application startup
Attempts:
2 left
💡 Hint
Think about how scope affects instance creation in NestJS.