Bird
Raised Fist0
Angularframework~20 mins

Enums in Angular applications - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Angular Enums Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Angular handle enum values in templates?
Given this enum and component code, what will be displayed in the template?

export enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

@Component({
  selector: 'app-status',
  template: `<p>Status: {{ status }}</p>`
})
export class StatusComponent {
  status = Status.Active;
}
Angular
export enum Status {
  Active = 'ACTIVE',
  Inactive = 'INACTIVE'
}

@Component({
  selector: 'app-status',
  template: `<p>Status: {{ status }}</p>`
})
export class StatusComponent {
  status = Status.Active;
}
AStatus: ACTIVE
BStatus: 0
CStatus: Active
DStatus: undefined
Attempts:
2 left
💡 Hint
Remember enums with string values show the string, not the key name.
📝 Syntax
intermediate
2:00remaining
Which enum declaration is valid in Angular TypeScript?
Choose the correct enum declaration syntax that will compile without errors in Angular.
Aenum Colors = { Red: 'RED', Green: 'GREEN', Blue: 'BLUE' }
Benum Colors { Red: 'RED', Green: 'GREEN', Blue: 'BLUE' }
Cenum Colors { Red = 'RED', Green = 'GREEN', Blue = 'BLUE' }
Denum Colors { 'Red' = 'RED', 'Green' = 'GREEN', 'Blue' = 'BLUE' }
Attempts:
2 left
💡 Hint
Look for the correct enum key-value syntax in TypeScript.
🔧 Debug
advanced
2:00remaining
Why does this Angular component throw an error when using an enum in the template?
Consider this code snippet:

export enum Mode {
  Light,
  Dark
}

@Component({
  selector: 'app-theme',
  template: `<p>Current mode: {{ Mode.Dark }}</p>`
})
export class ThemeComponent {
}

What is the cause of the error when rendering the template?
Angular
export enum Mode {
  Light,
  Dark
}

@Component({
  selector: 'app-theme',
  template: `<p>Current mode: {{ Mode.Dark }}</p>`
})
export class ThemeComponent {
}
AThe enum Mode is shadowed by the class property, causing a runtime error.
BThe template syntax {{ Mode.Dark }} is invalid and should be {{ Mode['Dark'] }}.
CThe enum values are numbers, so Angular cannot display them directly in templates.
DThe enum Mode is not accessible in the template because it is not assigned to a class property.
Attempts:
2 left
💡 Hint
Think about how templates access class properties and variables.
❓ state_output
advanced
2:00remaining
What is the value of 'currentStatus' after this Angular component runs?
Given this enum and component code:

export enum Status {
  Pending = 1,
  Approved,
  Rejected
}

@Component({
  selector: 'app-request',
  template: `<p>Status: {{ currentStatus }}</p>`
})
export class RequestComponent {
  currentStatus = Status.Approved;
}

What is the value of 'currentStatus'?
Angular
export enum Status {
  Pending = 1,
  Approved,
  Rejected
}

@Component({
  selector: 'app-request',
  template: `<p>Status: {{ currentStatus }}</p>`
})
export class RequestComponent {
  currentStatus = Status.Approved;
}
AApproved
B2
C1
Dundefined
Attempts:
2 left
💡 Hint
Numeric enums auto-increment from the first value.
🧠 Conceptual
expert
2:00remaining
Why should enums be assigned to component properties for template use in Angular?
In Angular, why is it necessary to assign an enum to a component property before using it in the template?
ABecause Angular templates can only access instance properties, not imported symbols directly.
BBecause enums are not supported in Angular templates unless converted to strings first.
CBecause assigning enums to properties improves runtime performance by caching values.
DBecause Angular requires all variables in templates to be declared with @Input decorators.
Attempts:
2 left
💡 Hint
Think about the scope of variables accessible in Angular templates.

Practice

(1/5)
1. What is the main purpose of using enum in an Angular application?
easy
A. To group related constant values under a single name
B. To create dynamic variables that change at runtime
C. To define functions inside components
D. To style components with CSS

Solution

  1. Step 1: Understand what enums represent

    Enums are used to group related constant values, making code easier to read and maintain.
  2. Step 2: Compare with other options

    Options A, B, and C describe unrelated concepts like styling, dynamic variables, or functions, which enums do not handle.
  3. Final Answer:

    To group related constant values under a single name -> Option A
  4. Quick Check:

    Enums = group constants [OK]
Hint: Enums group fixed values for clarity and safety [OK]
Common Mistakes:
  • Thinking enums create dynamic or changeable variables
  • Confusing enums with functions or styling
  • Using enums for unrelated tasks like CSS
2. Which of the following is the correct syntax to declare an enum named UserRole with values Admin and User in Angular?
easy
A. UserRole enum { Admin, User }
B. enum UserRole = { Admin, User }
C. enum UserRole { Admin, User }
D. enum UserRole: { Admin, User }

Solution

  1. Step 1: Recall enum declaration syntax in TypeScript

    The correct syntax is enum Name { Value1, Value2 } without equals or colons.
  2. Step 2: Check each option

    enum UserRole { Admin, User } matches the correct syntax. Options B, C, and D use invalid symbols like '=', misplaced keywords, or colons.
  3. Final Answer:

    enum UserRole { Admin, User } -> Option C
  4. Quick Check:

    enum syntax = enum Name { ... } [OK]
Hint: Use 'enum Name { ... }' without '=' or ':' [OK]
Common Mistakes:
  • Adding '=' after enum name
  • Using ':' instead of braces
  • Misplacing the 'enum' keyword
3. Given the enum declaration:
enum Status { Active = 1, Inactive = 0 }

What will be the output of console.log(Status.Active)?
medium
A. 1
B. Active
C. 0
D. undefined

Solution

  1. Step 1: Understand enum value assignment

    Status.Active is explicitly assigned the value 1 in the enum.
  2. Step 2: Check what console.log prints

    Logging Status.Active prints its numeric value, which is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Enum value = number assigned [OK]
Hint: Enum member prints its assigned value [OK]
Common Mistakes:
  • Expecting the name 'Active' to print instead of value
  • Confusing 0 and 1 values
  • Assuming undefined if not string
4. What is wrong with this enum declaration in Angular?
enum Color { Red = 'red', Green, Blue = 'blue' }
medium
A. Enum values cannot be strings
B. Enum names must be lowercase
C. Enum must have at least three values
D. You cannot mix string and numeric enum values without explicit assignment

Solution

  1. Step 1: Analyze enum value types

    Mixing string and numeric enum members without explicit values causes errors.
  2. Step 2: Identify the problem in the code

    Green has no assigned value after Red is string 'red', so it tries to auto-assign a number, which is invalid.
  3. Final Answer:

    You cannot mix string and numeric enum values without explicit assignment -> Option D
  4. Quick Check:

    Mixed enum types need explicit values [OK]
Hint: Assign all string values explicitly to avoid mixing errors [OK]
Common Mistakes:
  • Assuming enum names must be lowercase
  • Thinking enums can't have string values
  • Believing enums require minimum count
5. You want to use an enum OrderStatus in Angular to represent these states: Pending, Shipped, Delivered, and Cancelled. You also want to display user-friendly text for each status in your template. Which approach is best?
hard
A. Use only strings in enum and display enum keys directly
B. Define enum with numeric values and create a mapping object for display text
C. Use separate string constants instead of enum
D. Use enum with mixed string and numeric values without mapping

Solution

  1. Step 1: Understand enum usage for fixed states

    Using numeric enums keeps code efficient and clear for logic.
  2. Step 2: Provide user-friendly text separately

    Mapping numeric enum values to display strings allows flexible, readable UI text.
  3. Step 3: Evaluate other options

    Using only strings or constants reduces type safety or clarity; mixed enums without mapping cause errors.
  4. Final Answer:

    Define enum with numeric values and create a mapping object for display text -> Option B
  5. Quick Check:

    Numeric enum + mapping = best practice [OK]
Hint: Use numeric enums plus a display map for UI text [OK]
Common Mistakes:
  • Using mixed enum types without mapping
  • Displaying enum keys directly without user-friendly text
  • Avoiding enums and using loose string constants
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard