Bird
Raised Fist0
Angularframework~20 mins

Structural vs attribute directives in Angular - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Identify the type of directive
Which of the following Angular directives is a structural directive that changes the DOM layout by adding or removing elements?
A*ngIf
B[ngStyle]
C(click)
D[ngClass]
Attempts:
2 left
💡 Hint
Structural directives usually start with an asterisk (*) and control the presence of elements.
❓ component_behavior
intermediate
1:30remaining
Effect of attribute directive on element
Given this Angular template snippet:
<div [ngClass]="{ 'highlight': isActive }">Hello</div>

What happens when isActive is true?
AThe <code>div</code> element is removed from the DOM.
BThe <code>highlight</code> CSS class is added to the <code>div</code> element.
CThe <code>div</code> element is replaced with a <code>span</code> element.
DThe <code>div</code> element's inline style changes to <code>color: red</code>.
Attempts:
2 left
💡 Hint
Attribute directives change appearance or behavior without changing the DOM structure.
📝 Syntax
advanced
1:30remaining
Correct syntax for structural directive
Which option shows the correct Angular syntax to conditionally display a paragraph using a structural directive?
A<p *ngIf="showParagraph">This is a paragraph.</p>
B<p [ngIf]="showParagraph">This is a paragraph.</p>
C<p (ngIf)="showParagraph">This is a paragraph.</p>
D<p #ngIf="showParagraph">This is a paragraph.</p>
Attempts:
2 left
💡 Hint
Structural directives use an asterisk (*) before their name.
🔧 Debug
advanced
2:00remaining
Why does this structural directive cause an error?
Consider this Angular template code:
<div *ngFor="let item of items">{{ item }}</div>
<div *ngFor="items">{{ item }}</div>

Why does the second div cause an error?
ABecause *ngFor only works with arrays of numbers.
BBecause *ngFor cannot be used on <code>div</code> elements.
CBecause <code>items</code> is not defined in the component.
DBecause *ngFor requires a <code>let</code> variable declaration to iterate over the list.
Attempts:
2 left
💡 Hint
Check the syntax required by *ngFor to loop over a list.
❓ state_output
expert
2:00remaining
Output after toggling structural directive
Given this Angular component template and initial state:
<button (click)="show = !show">Toggle</button>
<div *ngIf="show">Visible</div>

Initially, show is false. What is the rendered output after clicking the button twice?
AThe <code>div</code> with text 'Visible' is shown after two clicks.
BAn error occurs because *ngIf cannot toggle.
CNo <code>div</code> is visible after two clicks.
DThe <code>div</code> is visible only after the first click, then disappears after the second.
Attempts:
2 left
💡 Hint
Think about how toggling a boolean twice affects the condition.

Practice

(1/5)
1. Which of the following best describes a structural directive in Angular?
easy
A. It binds data to an element's property.
B. It changes the style of an element without altering the DOM structure.
C. It changes the layout by adding or removing elements from the DOM.
D. It listens to user events like clicks or key presses.

Solution

  1. Step 1: Understand what structural directives do

    Structural directives modify the DOM layout by adding or removing elements dynamically.
  2. Step 2: Compare with other directive types

    Attribute directives change appearance or behavior but do not add or remove elements.
  3. Final Answer:

    It changes the layout by adding or removing elements from the DOM. -> Option C
  4. Quick Check:

    Structural directive = changes DOM structure [OK]
Hint: Structural = add/remove elements; attribute = change style [OK]
Common Mistakes:
  • Confusing attribute directives with structural directives
  • Thinking attribute directives add or remove elements
  • Mixing event listeners with directive types
2. Which of the following is the correct syntax for using the structural directive that repeats elements in Angular?
easy
A. <div *ngFor="let item of items">{{item}}</div>
B. <div [ngFor]="items">{{item}}</div>
C. <div (ngFor)="items">{{item}}</div>
D. <div #ngFor="items">{{item}}</div>

Solution

  1. Step 1: Recall the syntax for the structural directive *ngFor

    The correct syntax uses an asterisk (*) before ngFor and a let variable to iterate.
  2. Step 2: Check each option's syntax

    Only <div *ngFor="let item of items">{{item}}</div> uses *ngFor="let item of items" which is correct.
  3. Final Answer:

    <div *ngFor="let item of items">{{item}}</div> -> Option A
  4. Quick Check:

    *ngFor syntax = <div *ngFor="let item of items">{{item}}</div> [OK]
Hint: Structural directives use * before directive name [OK]
Common Mistakes:
  • Using square brackets [] instead of * for structural directives
  • Using parentheses () which are for events
  • Using template reference (#) incorrectly
3. Given the following Angular template code, what will be rendered?
<ul>
  <li *ngIf="showItem">Item 1</li>
  <li>Item 2</li>
</ul>

Assuming showItem is false.
medium
A. No output, error occurs
B.
  • Item 1
  • Item 2
C.
    D.
    • Item 2

    Solution

    1. Step 1: Understand *ngIf behavior with false condition

      *ngIf removes the element from the DOM if the condition is false.
    2. Step 2: Apply this to the template

      The first <li> with *ngIf="showItem" will not render because showItem is false. The second <li> renders normally.
    3. Final Answer:

      <ul><li>Item 2</li></ul> -> Option D
    4. Quick Check:

      *ngIf false removes element [OK]
    Hint: *ngIf false means element is removed, not hidden [OK]
    Common Mistakes:
    • Thinking *ngIf hides element but keeps it in DOM
    • Assuming both list items render regardless
    • Confusing *ngIf with attribute directives
    4. Identify the error in this Angular template snippet:
    <button *ngIf="isEnabled" [disabled]="isDisabled">Click Me</button>

    Assuming isEnabled and isDisabled are boolean variables.
    medium
    A. The syntax is correct; no error present.
    B. The *ngIf directive must be replaced with [ngIf].
    C. The [disabled] attribute should be replaced with *disabled.
    D. Using *ngIf and [disabled] together on the same element is invalid.

    Solution

    1. Step 1: Check compatibility of *ngIf and [disabled]

      *ngIf controls element presence; [disabled] controls attribute binding. Both can coexist.
    2. Step 2: Verify syntax correctness

      The syntax uses *ngIf with a boolean and [disabled] with a boolean, which is valid Angular syntax.
    3. Final Answer:

      The syntax is correct; no error present. -> Option A
    4. Quick Check:

      *ngIf and [disabled] can be used together [OK]
    Hint: *ngIf controls element, [disabled] controls attribute independently [OK]
    Common Mistakes:
    • Thinking *ngIf and attribute bindings cannot be combined
    • Confusing *ngIf with [ngIf]
    • Replacing attribute bindings with structural directive syntax
    5. You want to create a custom directive that highlights text by changing its background color when hovered. Which type of directive should you create and why?
    hard
    A. Structural directive, because it needs to add or remove elements on hover.
    B. Attribute directive, because it changes the element's appearance without altering layout.
    C. Structural directive, because it modifies the DOM structure dynamically.
    D. Attribute directive, because it listens to user events but does not change styles.

    Solution

    1. Step 1: Understand the directive's purpose

      The directive changes background color on hover, affecting appearance only.
    2. Step 2: Choose directive type based on behavior

      Attribute directives change element style or behavior without adding/removing elements, fitting this case.
    3. Final Answer:

      Attribute directive, because it changes the element's appearance without altering layout. -> Option B
    4. Quick Check:

      Appearance change = attribute directive [OK]
    Hint: Appearance changes use attribute directives, layout changes use structural [OK]
    Common Mistakes:
    • Choosing structural directive for style changes
    • Confusing event listening with directive type
    • Thinking attribute directives add/remove elements
    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