0
0
NestJSframework~20 mins

CRUD operations in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS CRUD 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 NestJS controller method?
Consider this NestJS controller method that handles a GET request to fetch all items. What will be the HTTP response body when this method is called?
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  findAll() {
    return ['apple', 'banana', 'cherry'];
  }
}
AHTTP 200 with JSON body: ["apple", "banana", "cherry"]
BHTTP 500 Internal Server Error
CHTTP 404 Not Found error
DHTTP 200 with empty JSON array []
Attempts:
2 left
💡 Hint
The method returns a simple array directly.
state_output
intermediate
2:00remaining
What is the value of the 'items' array after these service calls?
Given this NestJS service managing an in-memory list, what is the content of the 'items' array after executing the code below?
NestJS
export class ItemsService {
  private items = ['a', 'b'];

  addItem(item: string) {
    this.items.push(item);
  }

  removeItem(item: string) {
    this.items = this.items.filter(i => i !== item);
  }

  getItems() {
    return this.items;
  }
}

const service = new ItemsService();
service.addItem('c');
service.removeItem('a');
const result = service.getItems();
A["a", "b", "c"]
B["c"]
C["a", "b"]
D["b", "c"]
Attempts:
2 left
💡 Hint
Adding 'c' then removing 'a' affects the array.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this NestJS controller method?
Identify the option that will cause a syntax error when defining a POST method in a NestJS controller.
NestJS
import { Controller, Post, Body } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() userData) {
    return userData;
  }
}
A
}
;ataDresu nruter  
{ )ataDresu )(ydoB@(resUetaerc
)(tsoP@
B
@Post()
createUser(@Body() userData) {
  return userData;
}
C
@Post()
createUser(@Body userData) {
  return userData;
}
D
@Post()
createUser(@Body() userData) {
  return userData
}
Attempts:
2 left
💡 Hint
Check the decorator syntax carefully.
🔧 Debug
advanced
2:00remaining
Why does this NestJS service method throw a runtime error?
This service method is supposed to update an item by id. Why does it throw an error when called?
NestJS
export class ItemsService {
  private items = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }];

  updateItem(id: number, newName: string) {
    const item = this.items.find(i => i.id === id);
    item.name = newName;
  }
}

const service = new ItemsService();
service.updateItem(3, 'c');
AReferenceError: item is not defined
BTypeError: Cannot set property 'name' of undefined
CNo error, item updated successfully
DSyntaxError: Unexpected token '='
Attempts:
2 left
💡 Hint
What happens if find() returns undefined?
🧠 Conceptual
expert
2:00remaining
Which option correctly describes the role of DTOs in NestJS CRUD operations?
In NestJS, Data Transfer Objects (DTOs) are often used in CRUD operations. Which statement best describes their purpose?
ADTOs define the shape of data sent over the network and help validate input before processing.
BDTOs are used to directly access the database without services.
CDTOs automatically generate database tables based on controller methods.
DDTOs replace controllers and handle HTTP requests themselves.
Attempts:
2 left
💡 Hint
Think about how data is structured and validated in NestJS.