Bird
0
0

Given this Angular service and component code, what will be logged?

medium📝 component behavior Q13 of 15
Angular - Services and Dependency Injection
Given this Angular service and component code, what will be logged?
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class LoggerService {
  log(message: string) {
    console.log('Log:', message);
  }
}

import { Component } from '@angular/core';
import { LoggerService } from './logger.service';

@Component({ selector: 'app-root', template: '' })
export class AppComponent {
  constructor(private logger: LoggerService) {
    this.logger.log('Hello');
  }
}
ALog: Hello
BError: LoggerService not found
CNo output
DLog: undefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand service injection in constructor

    The LoggerService is injected into AppComponent via constructor and used to log a message.
  2. Step 2: Analyze the log call

    The log method prints 'Log:' plus the message 'Hello', so console shows 'Log: Hello'.
  3. Final Answer:

    Log: Hello -> Option A
  4. Quick Check:

    Injected service logs message correctly [OK]
Quick Trick: Injected services work via constructor parameters [OK]
Common Mistakes:
MISTAKES
  • Expecting error if service is providedIn root
  • Thinking log method won't run without template
  • Confusing console output with return value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes