Bird
Raised Fist0
Angularframework~20 mins

Computed signals for derived values in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Computed Signals Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular computed signal example?
Consider this Angular standalone component using signals and computed signals. What will be displayed inside the paragraph?
Angular
import { Component, computed, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `<p>{{ fullName() }}</p>`
})
export class AppComponent {
  firstName = signal('Jane');
  lastName = signal('Doe');
  fullName = computed(() => `${this.firstName()} ${this.lastName()}`);
}
AError: fullName is not a function
BJane Doe
CDoe
DJane
Attempts:
2 left
💡 Hint
Remember that computed signals are functions that return the derived value.
state_output
intermediate
2:00remaining
What is the value of 'fullName' after updating signals?
Given this Angular component, what is the value returned by fullName() after the lastName signal is updated?
Angular
import { Component, computed, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `{{ fullName() }}`
})
export class AppComponent {
  firstName = signal('John');
  lastName = signal('Smith');
  fullName = computed(() => `${this.firstName()} ${this.lastName()}`);

  constructor() {
    this.lastName.set('Brown');
  }
}
AJohn Brown
BJohn Smith
CBrown
DJohn
Attempts:
2 left
💡 Hint
Signals update reactively, so computed signals reflect the latest values.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a computed signal for the sum of two signals?
You want to create a computed signal that returns the sum of two number signals: countA and countB. Which code snippet is correct?
Asum = computed(() => countA() + countB);
Bsum = computed(() => countA + countB);
Csum = computed(() => this.countA() + this.countB());
Dsum = computed(() => this.countA + this.countB());
Attempts:
2 left
💡 Hint
Remember to call signals as functions to get their current values.
🔧 Debug
advanced
2:00remaining
Why does this computed signal not update when signals change?
Look at this Angular component code. Why does fullName not update when firstName or lastName change?
Angular
import { Component, computed, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `{{ fullName() }}`
})
export class AppComponent {
  firstName = signal('Alice');
  lastName = signal('Wonderland');
  fullName = computed(() => this.firstName + ' ' + this.lastName);

  constructor() {
    this.firstName.set('Bob');
  }
}
ABecause fullName is missing a return statement.
BBecause signals cannot be used inside computed signals.
CBecause the constructor updates firstName before fullName is defined.
DBecause signals are not called as functions inside computed, so dependencies are not tracked.
Attempts:
2 left
💡 Hint
Check how signals are accessed inside the computed function.
🧠 Conceptual
expert
2:00remaining
What happens if a computed signal depends on another computed signal that throws an error?
In Angular signals, if computed signal A depends on computed signal B, and B throws an error during evaluation, what is the behavior of A when accessed?
AAccessing A will also throw the same error because it depends on B.
BA will return undefined silently without throwing an error.
CA will cache the last successful value and not throw an error.
DA will ignore B's error and return a default value.
Attempts:
2 left
💡 Hint
Think about how errors propagate in reactive computed chains.

Practice

(1/5)
1. What is the main purpose of a computed signal in Angular's signal system?
easy
A. To replace all signals with a single global state
B. To store static data that never changes
C. To automatically update a value based on other signals when they change
D. To manually trigger UI updates

Solution

  1. Step 1: Understand what computed signals do

    Computed signals derive their value from other signals and update automatically when those signals change.
  2. Step 2: Compare options with this behavior

    Only To automatically update a value based on other signals when they change describes automatic updates based on dependencies, which matches computed signals.
  3. Final Answer:

    To automatically update a value based on other signals when they change -> Option C
  4. Quick Check:

    Computed signals = auto-update derived values [OK]
Hint: Computed signals auto-update when dependencies change [OK]
Common Mistakes:
  • Thinking computed signals store static data
  • Confusing manual updates with automatic updates
  • Assuming computed signals replace all signals
2. Which of the following is the correct syntax to create a computed signal in Angular?
easy
A. const total = computed(() => price() + tax());
B. const total = signal(() => price + tax);
C. const total = computed(price + tax);
D. const total = signal(price() + tax());

Solution

  1. Step 1: Recall computed signal syntax

    Computed signals use computed(() => ...) with a function returning the derived value.
  2. Step 2: Check each option

    const total = computed(() => price() + tax()); correctly uses computed(() => price() + tax()). const total = signal(() => price + tax); wrongly uses signal and no function. const total = computed(price + tax); misses the function wrapper. const total = signal(price() + tax()); uses signal instead of computed.
  3. Final Answer:

    const total = computed(() => price() + tax()); -> Option A
  4. Quick Check:

    Computed syntax = computed(() => value) [OK]
Hint: Use computed(() => ...) with a function for derived values [OK]
Common Mistakes:
  • Using signal() instead of computed() for derived values
  • Passing expressions directly without a function
  • Not calling dependent signals as functions
3. Given the code below, what will be logged after count.set(5) is called?
const count = signal(0);
const double = computed(() => count() * 2);

console.log(double());
count.set(5);
console.log(double());
medium
A. 0 then 10
B. 0 then 0
C. NaN then 10
D. Error at runtime

Solution

  1. Step 1: Evaluate initial values

    Initially, count is 0, so double() returns 0 * 2 = 0.
  2. Step 2: After count.set(5)

    Setting count to 5 updates double automatically. Calling double() now returns 5 * 2 = 10.
  3. Final Answer:

    0 then 10 -> Option A
  4. Quick Check:

    Initial double=0, after update=10 [OK]
Hint: Computed updates automatically after signal changes [OK]
Common Mistakes:
  • Assuming computed does not update after set()
  • Forgetting to call signals as functions
  • Expecting errors due to missing subscriptions
4. Identify the error in the following code snippet using computed signals:
const price = signal(100);
const tax = signal(0.1);
const total = computed(() => price + tax * price);

console.log(total());
medium
A. Computed signals cannot use arithmetic operations
B. Signals must be called as functions inside computed
C. Signals cannot be used inside computed
D. Missing initial value for tax signal

Solution

  1. Step 1: Check how signals are accessed

    Signals are functions and must be called with () to get their current value.
  2. Step 2: Analyze the computed expression

    The expression uses price and tax directly without calling them, so it uses the signal objects, not their values.
  3. Final Answer:

    Signals must be called as functions inside computed -> Option B
  4. Quick Check:

    Access signals with () inside computed [OK]
Hint: Always call signals as functions inside computed [OK]
Common Mistakes:
  • Using signal variables directly without ()
  • Thinking computed disallows arithmetic
  • Ignoring signal initial values
5. You want to create a computed signal that returns the full name by combining two signals: firstName and lastName. Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?
hard
A. const fullName = computed(() => firstName() + lastName());
B. const fullName = signal(`${firstName()} ${lastName()}`);
C. const fullName = computed(() => firstName + ' ' + lastName);
D. const fullName = computed(() => `${firstName()} ${lastName()}`);

Solution

  1. Step 1: Understand the goal

    The computed signal should combine firstName and lastName signals and update automatically when either changes.
  2. Step 2: Evaluate each option

    const fullName = computed(() => `${firstName()} ${lastName()}`); correctly calls both signals as functions and concatenates with a space. const fullName = signal(`${firstName()} ${lastName()}`); uses signal which won't update automatically. const fullName = computed(() => firstName + ' ' + lastName); uses signal variables directly without calling them. const fullName = computed(() => firstName() + lastName()); concatenates without space.
  3. Final Answer:

    const fullName = computed(() => `${firstName()} ${lastName()}`); -> Option D
  4. Quick Check:

    Call signals with () and combine with space [OK]
Hint: Use computed with template literals calling signals () [OK]
Common Mistakes:
  • Using signal() instead of computed() for derived values
  • Not calling signals as functions
  • Forgetting space between names