Bird
0
0

Given these services, what will OrderService.getOrderDetails() return?

medium📝 Predict Output Q4 of 15
Angular - Services and Dependency Injection
Given these services, what will OrderService.getOrderDetails() return?
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class ProductService {
  getProduct() { return 'Book'; }
}
@Injectable({ providedIn: 'root' })
export class OrderService {
  constructor(private productService: ProductService) {}
  getOrderDetails() {
    return `Order includes: ${this.productService.getProduct()}`;
  }
}
A"Order includes: null"
B"Order includes: undefined"
C"Order includes: Book"
DError: productService is not defined
Step-by-Step Solution
Solution:
  1. Step 1: Understand service injection and method call

    OrderService injects ProductService and calls getProduct(), which returns 'Book'.
  2. Step 2: Evaluate return value of getOrderDetails()

    It returns a string with 'Order includes: ' plus the product name 'Book'. No errors expected.
  3. Final Answer:

    "Order includes: Book" -> Option C
  4. Quick Check:

    Injected service method output = 'Book' [OK]
Quick Trick: Injected service methods return expected values [OK]
Common Mistakes:
MISTAKES
  • Assuming injected service is undefined
  • Expecting runtime error due to injection
  • Confusing null with undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes