Bird
0
0

Given these services, what will DataService.getData() return?

medium📝 component behavior Q13 of 15
Angular - Services and Dependency Injection
Given these services, what will DataService.getData() return?
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class LoggerService {
  log() { return 'Logged'; }
}

@Injectable({ providedIn: 'root' })
export class DataService {
  constructor(private logger: LoggerService) {}
  getData() {
    return this.logger.log() + ' Data';
  }
}
AError: logger is undefined
B'Data Logged'
C'Logged'
D'Logged Data'
Step-by-Step Solution
Solution:
  1. Step 1: Understand service injection and method calls

    DataService injects LoggerService and calls its log() method, which returns 'Logged'.
  2. Step 2: Analyze getData() return value

    getData() returns the string from logger.log() plus ' Data', so the result is 'Logged Data'.
  3. Final Answer:

    'Logged Data' -> Option D
  4. Quick Check:

    Injected method output + ' Data' = 'Logged Data' [OK]
Quick Trick: Injected service method output concatenated with string [OK]
Common Mistakes:
MISTAKES
  • Assuming logger is undefined due to missing initialization
  • Mixing order of concatenated strings
  • Forgetting to call the log() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes