0
0
Angularframework~20 mins

Standalone component declaration in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Standalone Master
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 standalone Angular component?
Consider this Angular standalone component code. What will be rendered in the browser?
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `<button (click)="count.set(count() + 1)">Clicked {{ count() }} times</button>`
})
export class CounterComponent {
  count = signal(0);
}
ANo button is rendered because standalone components need to be declared in a module.
BA button showing 'Clicked undefined times' and does not update on click.
CA button showing 'Clicked NaN times' and does not update on click.
DA button showing 'Clicked 0 times' initially, increments count on each click.
Attempts:
2 left
💡 Hint
Standalone components can be used without modules and signals hold reactive state.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares a standalone Angular component?
Select the code snippet that correctly declares a standalone Angular component with a selector 'app-hello' and a template displaying 'Hello World'.
A
import { Component } from '@angular/core';
@Component({
  selector: 'app-hello',
  standalone: false,
  template: `&lt;p&gt;Hello World&lt;/p&gt;`
})
export class HelloComponent {}
B
import { Component } from '@angular/core';
@Component({
  selector: 'app-hello',
  template: `&lt;p&gt;Hello World&lt;/p&gt;`
})
export class HelloComponent {}
C
import { Component } from '@angular/core';
@Component({
  selector: 'app-hello',
  standalone: true,
  template: `&lt;p&gt;Hello World&lt;/p&gt;`
})
export class HelloComponent {}
D
import { Component } from '@angular/core';
@Component({
  selector: 'app-hello',
  standalone: true
})
export class HelloComponent {
  template = `&lt;p&gt;Hello World&lt;/p&gt;`;
}
Attempts:
2 left
💡 Hint
Standalone components require the 'standalone: true' property inside the @Component decorator and the template inside the decorator.
🔧 Debug
advanced
2:00remaining
Why does this standalone component fail to render?
Given this standalone component code, why does Angular fail to render it?
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-greet',
  standalone: true,
  template: `<h1>{{ greeting }}</h1>`
})
export class GreetComponent {
  greeting: string;

  constructor() {
    // Intentionally left blank
  }
}
AThe component renders an empty <h1> because 'greeting' is undefined, but no error occurs.
BStandalone components must use signals for reactive properties, so 'greeting' must be a signal.
CThe component lacks the 'imports' array in the decorator, causing a runtime error.
DThe 'greeting' property is not initialized, so it is undefined and Angular throws an error.
Attempts:
2 left
💡 Hint
Uninitialized class properties in Angular templates show as empty but do not cause errors.
lifecycle
advanced
2:00remaining
How to inject a service in a standalone component?
Which code snippet correctly injects a service named 'UserService' into a standalone Angular component using the new Angular 17+ inject() function?
A
import { Component } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `&lt;p&gt;User loaded&lt;/p&gt;`
})
export class UserComponent {
  static userService = inject(UserService);
}
B
import { Component, inject } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `&lt;p&gt;User loaded&lt;/p&gt;`
})
export class UserComponent {
  userService = inject(UserService);
}
C
import { Component, inject } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `&lt;p&gt;User loaded&lt;/p&gt;`
})
export class UserComponent {
  userService = new UserService();
}
D
import { Component } from '@angular/core';
import { UserService } from './user.service';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `&lt;p&gt;User loaded&lt;/p&gt;`
})
export class UserComponent {
  constructor(private userService: UserService) {}
}
Attempts:
2 left
💡 Hint
Use the inject() function inside the class body to get the service instance.
🧠 Conceptual
expert
2:00remaining
What is a key advantage of standalone components in Angular 17+?
Choose the most accurate statement about standalone components introduced in Angular 17+.
AThey eliminate the need for NgModules, simplifying component reuse and reducing boilerplate.
BThey disable dependency injection to improve startup time.
CThey force all components to use signals for state management, making legacy code incompatible.
DThey require all components to be declared in a single global module for better performance.
Attempts:
2 left
💡 Hint
Standalone components reduce the complexity of Angular apps by removing module declarations.