0
0
Angularframework~20 mins

*ngFor for list rendering in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NgFor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Angular template?
Given the component's items array and the template below, what will be the rendered output inside the <ul> element?
Angular
items = ['apple', 'banana', 'cherry'];

Template:
<ul>
  <li *ngFor="let item of items; let i = index">{{i}}: {{item}}</li>
</ul>
A<ul><li>0: apple</li><li>1: banana</li><li>2: cherry</li></ul>
B<ul><li>apple</li><li>banana</li><li>cherry</li></ul>
C<ul><li>0: apple</li><li>0: banana</li><li>0: cherry</li></ul>
D<ul><li>1: apple</li><li>2: banana</li><li>3: cherry</li></ul>
Attempts:
2 left
💡 Hint
Remember that index starts at 0 and is assigned to i.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses *ngFor with a trackBy function?
You want to optimize list rendering by using a trackBy function named trackById. Which template syntax is correct?
A<li *ngFor="let item of items; trackBy: trackById()">{{item.name}}</li>
B<li *ngFor="let item of items; trackByBy: trackById">{{item.name}}</li>
C<li *ngFor="let item of items; trackBy: trackById">{{item.name}}</li>
D<li *ngFor="let item of items; trackBy=trackById">{{item.name}}</li>
Attempts:
2 left
💡 Hint
The trackBy syntax uses a colon, not an equals sign or parentheses.
🔧 Debug
advanced
2:00remaining
Why does this *ngFor code cause a runtime error?
Consider this template snippet:
  • User: {{user.name}}, Odd: {{odd}}
The component has users = [{name: 'Anna'}, {name: 'Bob'}]; What is the cause of the runtime error?
AThe <code>user</code> variable is not declared properly.
BThe <code>users</code> array is empty, so <code>*ngFor</code> fails.
CThe syntax should use <code>let isOdd = odd</code> instead of <code>let odd = odd</code>.
D<code>let odd = odd</code> is invalid because <code>odd</code> conflicts with the predefined <code>*ngFor</code> local variable.
Attempts:
2 left
💡 Hint
Check the list of valid local variables *ngFor supports.
state_output
advanced
2:00remaining
What is the value of selectedItem after this template interaction?
Given this component code and template: Component: selectedItem = null; items = ['red', 'green', 'blue']; Template:
  • {{color}}

Selected: {{selectedItem}}

If the user clicks the second <li> (green), what is displayed in the paragraph?
ASelected: red
BSelected: green
CSelected: blue
DSelected: null
Attempts:
2 left
💡 Hint
Clicking an item sets selectedItem to that item's value.
🧠 Conceptual
expert
3:00remaining
Which statement about *ngFor and change detection is true?
Consider Angular's change detection and the *ngFor directive. Which statement correctly explains how trackBy affects performance?
AUsing <code>trackBy</code> prevents Angular from recreating DOM elements when the list changes if the tracked identity stays the same.
B<code>trackBy</code> forces Angular to recreate all DOM elements every time the list changes.
CWithout <code>trackBy</code>, Angular never updates the DOM when the list changes.
D<code>trackBy</code> disables Angular's change detection for the entire component.
Attempts:
2 left
💡 Hint
Think about how Angular identifies list items to optimize rendering.