0
0
Angularframework~20 mins

Pure vs impure pipes in Angular - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipe Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a pure pipe behave with unchanged input?
Consider an Angular pure pipe that transforms an input value. If the input object reference does not change, what will happen when Angular runs change detection?
AAngular throws an error because the input did not change.
BThe pipe will run again and produce a new output every time.
CThe pipe will not run again and the previous output is reused.
DThe pipe runs only if the input value is a primitive type.
Attempts:
2 left
💡 Hint
Pure pipes run only when the input reference changes.
component_behavior
intermediate
2:00remaining
What triggers an impure pipe to run?
An Angular impure pipe is used in a template. When does Angular call the transform method of this impure pipe?
AEvery time change detection runs, regardless of input changes.
BOnly when the input reference changes.
COnly when the component is initialized.
DOnly when the input is a primitive value.
Attempts:
2 left
💡 Hint
Impure pipes run on every change detection cycle.
state_output
advanced
2:30remaining
Output of pure pipe with mutated input object
Given a pure pipe that formats an array input, what will be the output if the array is mutated (e.g., an item is pushed) but the array reference stays the same?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'joinArray', pure: true })
export class JoinArrayPipe implements PipeTransform {
  transform(value: string[]): string {
    console.log('Pipe transform called');
    return value.join(', ');
  }
}

// In component template:
// {{ myArray | joinArray }}

// In component class:
// myArray = ['apple', 'banana'];
// After some event:
// myArray.push('cherry');
AAngular throws a runtime error because the array was mutated.
BThe pipe transform method is NOT called again; output remains 'apple, banana'.
CThe pipe transform method is called but output does not change.
DThe pipe transform method is called again; output updates to 'apple, banana, cherry'.
Attempts:
2 left
💡 Hint
Pure pipes check input references, not internal mutations.
📝 Syntax
advanced
2:00remaining
Correct way to declare an impure pipe
Which of the following code snippets correctly declares an Angular pipe as impure?
A
@Pipe({ name: 'examplePipe' })
export class ExamplePipe implements PipeTransform {
  transform(value: any) { return value; }
}
B
@Pipe({ name: 'examplePipe', impure: true })
export class ExamplePipe implements PipeTransform {
  transform(value: any) { return value; }
}
C
@Pipe({ name: 'examplePipe', pure: true })
export class ExamplePipe implements PipeTransform {
  transform(value: any) { return value; }
}
D
@Pipe({ name: 'examplePipe', pure: false })
export class ExamplePipe implements PipeTransform {
  transform(value: any) { return value; }
}
Attempts:
2 left
💡 Hint
Impure pipes are declared by setting pure to false.
🔧 Debug
expert
3:00remaining
Why does this impure pipe not update output?
An Angular impure pipe is used to display the length of an array. The array is mutated by pushing new items, but the displayed length does not update. What is the most likely cause?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'lengthPipe', pure: false })
export class LengthPipe implements PipeTransform {
  transform(value: any[]): number {
    return value.length;
  }
}

// In component template:
// {{ myArray | lengthPipe }}

// In component class:
// myArray = ['a', 'b'];
// someMethod() {
//   this.myArray.push('c');
// }
AThe component does not trigger change detection after mutating the array.
BThe transform method is missing a return statement.
CThe pipe must be pure to detect mutations.
DImpure pipes do not detect changes in array length.
Attempts:
2 left
💡 Hint
Impure pipes run every change detection cycle, but change detection must run.