Bird
0
0

Given the code below, what will be the output of app.getService('UserService').getUserName()?

medium📝 Analysis Q4 of 15
LLD - Advanced LLD Concepts
Given the code below, what will be the output of app.getService('UserService').getUserName()?
class UserService {
  constructor(logger) {
    this.logger = logger;
  }
  getUserName() {
    this.logger.log('Fetching user');
    return 'Alice';
  }
}
class Logger {
  log(msg) {
    console.log('LOG:', msg);
  }
}
const container = new Container();
container.register('Logger', Logger);
container.register('UserService', UserService, ['Logger']);
const app = container.build();
AError: Logger not injected
BLOG: Fetching user
CAlice
Dundefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand service registration and injection

    UserService depends on Logger. Container injects Logger instance when creating UserService.
  2. Step 2: Analyze method call output

    getUserName logs a message and returns 'Alice'. The console log is side effect, return value is 'Alice'.
  3. Final Answer:

    Alice -> Option C
  4. Quick Check:

    Service method output = Alice [OK]
Quick Trick: Injected dependencies enable method calls to work correctly [OK]
Common Mistakes:
  • Confusing console log output with return value
  • Assuming injection failed
  • Expecting undefined due to missing constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes