Bird
Raised Fist0
NestJSframework~20 mins

Application lifecycle in NestJS - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. Which lifecycle hook in NestJS is used to run code right after a module has been initialized?
easy
A. OnModuleInit
B. OnApplicationBootstrap
C. OnApplicationShutdown
D. OnModuleDestroy

Solution

  1. Step 1: Understand module initialization timing

    The OnModuleInit hook runs immediately after the module's dependencies are resolved and the module is set up.
  2. Step 2: Match the hook to the description

    OnApplicationBootstrap runs after the entire app starts, OnApplicationShutdown runs before app stops, and OnModuleDestroy runs before module is destroyed.
  3. Final Answer:

    OnModuleInit -> Option A
  4. Quick Check:

    Run code after module setup = OnModuleInit [OK]
Hint: Module init code runs in OnModuleInit hook [OK]
Common Mistakes:
  • Confusing OnModuleInit with OnApplicationBootstrap
  • Using OnApplicationShutdown for startup code
  • Thinking OnModuleDestroy runs after module init
2. Which of the following is the correct way to implement the OnApplicationShutdown lifecycle hook in a NestJS service?
easy
A. export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } }
B. export class MyService implements OnModuleInit { onApplicationShutdown() { /* code */ } }
C. export class MyService implements OnApplicationBootstrap { onApplicationShutdown(signal: number) { /* code */ } }
D. export class MyService implements OnModuleDestroy { onApplicationShutdown(signal?: string) { /* code */ } }

Solution

  1. Step 1: Identify correct interface and method signature

    The OnApplicationShutdown interface requires implementing onApplicationShutdown(signal?: string) method.
  2. Step 2: Check method name and parameter types

    export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } } correctly implements OnApplicationShutdown with optional string parameter signal. Others mismatch interface or method name.
  3. Final Answer:

    export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } } -> Option A
  4. Quick Check:

    Correct interface and method signature = export class MyService implements OnApplicationShutdown { onApplicationShutdown(signal?: string) { /* code */ } } [OK]
Hint: Match interface name with method signature exactly [OK]
Common Mistakes:
  • Implementing wrong interface for shutdown hook
  • Using wrong method name or parameters
  • Confusing OnApplicationShutdown with OnModuleInit
3. Consider this NestJS service code snippet:
export class AppService implements OnModuleInit, OnApplicationShutdown {
  onModuleInit() {
    console.log('Module initialized');
  }
  onApplicationShutdown(signal?: string) {
    console.log('App shutting down:', signal);
  }
}

What will be printed if the application starts and then receives a SIGINT signal to stop?
medium
A. Module initialized only
B. App shutting down: SIGINT\nModule initialized
C. App shutting down: SIGINT only
D. Module initialized\nApp shutting down: SIGINT

Solution

  1. Step 1: Understand lifecycle hook order on start

    onModuleInit runs after module setup, so 'Module initialized' prints on app start.
  2. Step 2: Understand shutdown hook on SIGINT

    When SIGINT signal is received, onApplicationShutdown runs and logs 'App shutting down: SIGINT'.
  3. Final Answer:

    Module initialized\nApp shutting down: SIGINT -> Option D
  4. Quick Check:

    Start logs init, stop logs shutdown with signal [OK]
Hint: Startup logs OnModuleInit, shutdown logs OnApplicationShutdown [OK]
Common Mistakes:
  • Assuming shutdown logs before startup
  • Missing signal parameter in shutdown log
  • Thinking only one hook runs
4. This NestJS service code has an error:
export class MyService implements OnApplicationShutdown {
  onApplicationShutdown() {
    console.log('Cleaning up');
  }
}

What is the error and how to fix it?
medium
A. Should implement OnModuleDestroy instead of OnApplicationShutdown
B. Wrong method name; should be onShutdown instead
C. Missing optional signal parameter in onApplicationShutdown method
D. No error; code is correct

Solution

  1. Step 1: Check method signature for OnApplicationShutdown

    The interface requires onApplicationShutdown(signal?: string) with an optional string parameter.
  2. Step 2: Identify missing parameter in method

    The method lacks the optional signal parameter, causing a signature mismatch error.
  3. Final Answer:

    Missing optional signal parameter in onApplicationShutdown method -> Option C
  4. Quick Check:

    Method signature must match interface including optional parameters [OK]
Hint: Include optional signal parameter in onApplicationShutdown method [OK]
Common Mistakes:
  • Omitting optional signal parameter
  • Using wrong method name
  • Confusing OnApplicationShutdown with OnModuleDestroy
5. You want to perform cleanup tasks only when the app shuts down due to a SIGTERM signal, ignoring other signals. How should you implement this in a NestJS service?
hard
A. Override the main bootstrap function to handle SIGTERM manually
B. Implement OnApplicationShutdown and check if signal === 'SIGTERM' inside onApplicationShutdown method
C. Use OnApplicationBootstrap to detect shutdown signals
D. Implement OnModuleDestroy and check for SIGTERM signal there

Solution

  1. Step 1: Choose correct lifecycle hook for app shutdown

    OnApplicationShutdown runs when the app is stopping and receives the shutdown signal.
  2. Step 2: Filter signal inside onApplicationShutdown method

    Inside onApplicationShutdown(signal?: string), check if signal === 'SIGTERM' to run cleanup only for that signal.
  3. Final Answer:

    Implement OnApplicationShutdown and check if signal === 'SIGTERM' inside onApplicationShutdown method -> Option B
  4. Quick Check:

    Filter shutdown signal inside OnApplicationShutdown hook [OK]
Hint: Check signal inside OnApplicationShutdown to filter SIGTERM [OK]
Common Mistakes:
  • Using OnModuleDestroy which runs on module destroy, not app shutdown
  • Trying to detect shutdown in OnApplicationBootstrap
  • Manually overriding bootstrap instead of lifecycle hooks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard