0
0
NestJSframework~20 mins

Default value pipe in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Value Pipe 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 using DefaultValuePipe with a missing query parameter?

Consider this NestJS controller method:

  @Get('items')
  getItems(@Query('limit', new DefaultValuePipe('10')) limit: string) {
    return limit;
  }

If a client calls /items without the limit query parameter, what will be the response?

A"10"
Bundefined
Cnull
D0
Attempts:
2 left
💡 Hint

Think about what DefaultValuePipe does when the parameter is missing.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies DefaultValuePipe to a route parameter?

Given this NestJS route handler, which option correctly applies DefaultValuePipe to the id route parameter with a default of '42'?

  @Get(':id')
  getById( /* what goes here? */ ) {
    return id;
  }
A@Param('id', new DefaultValuePipe(42)) id: number
B@Param('id', new DefaultValuePipe('42')) id: string
C@Param('id', DefaultValuePipe('42')) id: string
D@Param('id', new DefaultValuePipe) id: string = '42'
Attempts:
2 left
💡 Hint

Remember how to instantiate pipes and pass default values.

🔧 Debug
advanced
2:00remaining
Why does this DefaultValuePipe usage cause a runtime error?

Examine this code snippet:

  @Get()
  getData(@Query('page', new DefaultValuePipe(1)) page: number) {
    return page + 1;
  }

When calling /endpoint without the page query parameter, a runtime error occurs. Why?

AThe query parameter 'page' is required and cannot have a default.
BDefaultValuePipe cannot accept numbers as default values, only strings.
CThe pipe is not instantiated correctly; it needs parentheses after the class name.
DDefaultValuePipe returns a string '1', but page is typed as number, causing a type error when adding.
Attempts:
2 left
💡 Hint

Consider the type of the default value returned by DefaultValuePipe.

🧠 Conceptual
advanced
2:00remaining
What happens if DefaultValuePipe is combined with ParseIntPipe on a query parameter?

Consider this NestJS method:

  @Get()
  getItems(
    @Query('limit', new DefaultValuePipe('5'), new ParseIntPipe()) limit: number
  ) {
    return limit * 2;
  }

If the client calls /items without the limit parameter, what will be the output?

A10
BNaN
C5
DError thrown by ParseIntPipe
Attempts:
2 left
💡 Hint

Think about the order pipes run and how ParseIntPipe converts strings.

state_output
expert
2:00remaining
What is the value of 'offset' after this NestJS controller method runs?

Given this method:

  @Get()
  getOffset(
    @Query('offset', new DefaultValuePipe('0'), new ParseIntPipe()) offset: number
  ) {
    return offset;
  }

If the client calls /endpoint?offset=abc, what happens?

Aoffset is 0 because DefaultValuePipe sets default and ParseIntPipe converts it
Boffset is NaN because 'abc' cannot be parsed to number
CParseIntPipe throws a BadRequestException due to invalid number
Doffset is the string 'abc' because DefaultValuePipe overrides
Attempts:
2 left
💡 Hint

What does ParseIntPipe do when it cannot parse a string to a number?