0
0
NestJSframework~20 mins

Application lifecycle in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
lifecycle
intermediate
2:00remaining
What is the output when using OnModuleInit in NestJS?
Consider this NestJS service implementing OnModuleInit. What will be logged when the module initializes?
NestJS
import { Injectable, OnModuleInit } from '@nestjs/common';

@Injectable()
export class MyService implements OnModuleInit {
  onModuleInit() {
    console.log('Module initialized');
  }
}
A'Module initialized' is logged every time a method in MyService is called
BAn error is thrown because OnModuleInit requires async method
C'Module initialized' is logged once when the module starts
DNothing is logged because onModuleInit is not called automatically
Attempts:
2 left
💡 Hint
OnModuleInit runs once when the module is ready.
component_behavior
intermediate
2:00remaining
What happens if you implement OnApplicationShutdown in a provider?
Given this provider implementing OnApplicationShutdown, what will happen when the app shuts down?
NestJS
import { Injectable, OnApplicationShutdown } from '@nestjs/common';

@Injectable()
export class ShutdownService implements OnApplicationShutdown {
  onApplicationShutdown(signal?: string) {
    console.log(`App shutting down due to: ${signal}`);
  }
}
AThe method is never called because shutdown hooks are disabled by default
BThe message logs with the shutdown signal when the app closes gracefully
CThe method runs repeatedly during app runtime causing performance issues
DAn error occurs because the signal parameter is required but not passed
Attempts:
2 left
💡 Hint
Shutdown hooks run when the app closes.
🔧 Debug
advanced
2:00remaining
Why does this NestJS lifecycle hook not run?
This service implements OnModuleDestroy but the method never runs. Why?
NestJS
import { Injectable, OnModuleDestroy } from '@nestjs/common';

@Injectable()
export class MyService implements OnModuleDestroy {
  onModuleDestroy() {
    console.log('Module is being destroyed');
  }
}

// The app never calls app.close()
ABecause app.close() is not called, OnModuleDestroy is never triggered
BBecause OnModuleDestroy requires async method signature
CBecause the service is not injected anywhere, so lifecycle hooks don't run
DBecause onModuleDestroy method name is case sensitive and should be onModuleDestroyHook
Attempts:
2 left
💡 Hint
Lifecycle destroy hooks run only when app closes.
📝 Syntax
advanced
2:00remaining
Which option correctly implements OnApplicationBootstrap?
Choose the correct syntax for implementing OnApplicationBootstrap in a NestJS service.
A
export class AppService implements OnApplicationBootstrap {
  onApplicationBootstrap(): void {
    console.log('App bootstrapped');
  }
}
B
export class AppService implements OnApplicationBootstrap {
  onApplicationBootstrap() {
    console.log('App bootstrapped');
  }
}
C
export class AppService implements OnApplicationBootstrap {
  onApplicationBootstrap(async) {
    console.log('App bootstrapped');
  }
}
D
export class AppService implements OnApplicationBootstrap {
  onApplicationBootstrap() => {
    console.log('App bootstrapped');
  }
}
Attempts:
2 left
💡 Hint
Method signature must be valid TypeScript.
🧠 Conceptual
expert
3:00remaining
What is the order of NestJS lifecycle hooks during app startup and shutdown?
Arrange the lifecycle hooks in the order they are called during app startup and shutdown.
A2,1,4,3
B2,1,3,4
C1,2,4,3
D1,2,3,4
Attempts:
2 left
💡 Hint
Module init happens before app bootstrap; shutdown hooks run before destroy hooks.