Bird
0
0

Given the code below, what will serviceA.getName() output?

medium📝 component behavior Q13 of 15
LLD - Advanced LLD Concepts
Given the code below, what will serviceA.getName() output?
class ServiceA {
  getName() { return 'Service A'; }
}

injector.register(ServiceA);
const serviceA = injector.get(ServiceA);
console.log(serviceA.getName());
Anull
Bundefined
CError: Service not found
DService A
Step-by-Step Solution
Solution:
  1. Step 1: Understand registration and retrieval

    The code registers ServiceA with the injector, then asks the injector to give an instance of ServiceA.
  2. Step 2: Check the method call on the instance

    The instance has a method getName() that returns the string 'Service A'. So calling serviceA.getName() returns 'Service A'.
  3. Final Answer:

    Service A -> Option D
  4. Quick Check:

    Registered service returns its name [OK]
Quick Trick: Registered services return their methods normally [OK]
Common Mistakes:
  • Assuming injector.get returns undefined or null
  • Forgetting to register before getting
  • Expecting an error without registration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes