Bird
0
0

Given this Angular service code, what will be removed by tree shaking if unusedMethod() is never called anywhere?

medium📝 component behavior Q13 of 15
Angular - Performance Optimization
Given this Angular service code, what will be removed by tree shaking if unusedMethod() is never called anywhere?
export class DataService {
  fetchData() { return 'data'; }
  unusedMethod() { return 'not used'; }
}
AOnly fetchData will be kept; unusedMethod will be removed
BBoth fetchData and unusedMethod will be kept
COnly unusedMethod will be kept; fetchData will be removed
DNeither method will be removed
Step-by-Step Solution
Solution:
  1. Step 1: Identify used and unused methods

    fetchData is assumed used; unusedMethod is never called anywhere.
  2. Step 2: Apply tree shaking effect

    Tree shaking removes unused code like unusedMethod to reduce bundle size.
  3. Final Answer:

    Only fetchData will be kept; unusedMethod will be removed -> Option A
  4. Quick Check:

    Unused code removed = Only fetchData will be kept; unusedMethod will be removed [OK]
Quick Trick: Unused methods get removed by tree shaking [OK]
Common Mistakes:
  • Assuming all methods stay regardless of usage
  • Confusing tree shaking with runtime errors
  • Thinking tree shaking removes used code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes