Bird
Raised Fist0
Angularframework~20 mins

Importing dependencies directly 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
🎖️
Angular Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct import syntax for Angular HttpClient
Which option correctly imports HttpClient from Angular's @angular/common/http package?
Aimport HttpClient from '@angular/http';
Bimport { HttpClient } from '@angular/common/http';
Cimport { HttpClientModule } from '@angular/common/http';
Dimport HttpClient from '@angular/common/http';
Attempts:
2 left
💡 Hint
Angular uses named imports for HttpClient from the common/http package.
component_behavior
intermediate
2:00remaining
Effect of missing import on Angular component
What happens if you forget to import FormsModule in an Angular module but use [(ngModel)] in a component template?
AThe app compiles and runs normally without any errors.
BThe app compiles but <code>ngModel</code> binding does not work and throws a runtime error.
CThe app fails to compile with a template parse error about <code>ngModel</code>.
DThe app compiles but <code>ngModel</code> silently ignores changes.
Attempts:
2 left
💡 Hint
Angular requires importing modules that provide template directives.
🔧 Debug
advanced
2:30remaining
Identify the import error causing compilation failure
Given this Angular component code, which import mistake causes a compilation error when injecting Router?
import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  template: ``
})
export class HomeComponent {
  constructor(private router: Router) {}
  goHome() { this.router.navigate(['/']); }
}
AImporting Router from '@angular/common' causes compilation error.
BImporting Router from '@angular/router' is correct; no error here.
CImporting Router from '@angular/core' causes compilation error.
DNot importing Router at all causes compilation error.
Attempts:
2 left
💡 Hint
Router must be imported from the correct Angular package.
🧠 Conceptual
advanced
2:30remaining
Why use standalone components with direct imports in Angular 17+?
What is the main advantage of importing dependencies directly in standalone Angular components instead of using NgModules?
AIt disables lazy loading of components.
BIt forces all components to share the same global scope.
CIt requires manually declaring components in NgModules.
DIt reduces boilerplate and improves tree-shaking for smaller bundles.
Attempts:
2 left
💡 Hint
Think about bundle size and code simplicity.
state_output
expert
3:00remaining
Output of Angular component with direct service import and injection
Consider this Angular service and component:
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class CounterService {
  count = 0;
  increment() { this.count++; }
}

import { Component } from '@angular/core';
import { CounterService } from './counter.service';

@Component({
  selector: 'app-counter',
  template: ``
})
export class CounterComponent {
  constructor(public counter: CounterService) {}
  increment() { this.counter.increment(); }
}
What will be displayed on the button after clicking it three times?
ACount: 3
BCount: undefined
CCount: 1
DCount: 0
Attempts:
2 left
💡 Hint
The service is singleton and state is shared.

Practice

(1/5)
1. What is the main purpose of importing dependencies directly in Angular?
easy
A. To create new Angular components automatically
B. To run Angular applications without a server
C. To use code from other files or libraries in your current file
D. To style components with CSS

Solution

  1. Step 1: Understand what importing means

    Importing means bringing code from other files or libraries into your current file so you can use it.
  2. Step 2: Identify the purpose in Angular

    In Angular, importing dependencies directly allows you to use components, services, or modules defined elsewhere.
  3. Final Answer:

    To use code from other files or libraries in your current file -> Option C
  4. Quick Check:

    Importing = Using external code [OK]
Hint: Importing means bringing code in from other files [OK]
Common Mistakes:
  • Thinking importing creates components automatically
  • Confusing importing with styling
  • Assuming importing runs the app
2. Which of the following is the correct syntax to import the HttpClient from Angular's common HTTP package?
easy
A. import { HttpClient } from '@angular/common/http';
B. import HttpClient from '@angular/common/http';
C. import (HttpClient) from '@angular/common/http';
D. import * HttpClient from '@angular/common/http';

Solution

  1. Step 1: Recall Angular import syntax

    Angular uses curly braces to import specific parts from a package or file.
  2. Step 2: Match syntax with options

    import { HttpClient } from '@angular/common/http'; uses curly braces correctly: import { HttpClient } from '@angular/common/http';
  3. Final Answer:

    import { HttpClient } from '@angular/common/http'; -> Option A
  4. Quick Check:

    Curly braces for specific imports [OK]
Hint: Use curly braces to import specific parts [OK]
Common Mistakes:
  • Omitting curly braces for named imports
  • Using parentheses instead of braces
  • Using incorrect import keywords
3. Given the following import statement in an Angular component:
import { Component } from '@angular/core';

What will happen if you remove the curly braces around Component?
medium
A. The import will work normally without errors
B. You will get a syntax error because named imports need curly braces
C. Angular will import the entire module instead
D. The component will import but not function correctly

Solution

  1. Step 1: Understand named imports syntax

    Named imports require curly braces to specify exactly what to import from a module.
  2. Step 2: Analyze removing curly braces effect

    Removing curly braces tries to import a default export, which @angular/core does not provide for Component, causing a syntax error.
  3. Final Answer:

    You will get a syntax error because named imports need curly braces -> Option B
  4. Quick Check:

    Named imports require braces [OK]
Hint: Named imports always need curly braces [OK]
Common Mistakes:
  • Assuming import works without braces
  • Confusing default and named imports
  • Thinking Angular auto-corrects import syntax
4. You wrote this import in your Angular component:
import { RouterModule } from '@angular/router';

But you get an error saying Cannot find module '@angular/router'. What is the most likely cause?
medium
A. RouterModule is not exported from @angular/router
B. You used curly braces incorrectly
C. You need to import RouterModule from @angular/core instead
D. You forgot to install the @angular/router package

Solution

  1. Step 1: Understand the error message

    The error means the module '@angular/router' is not found in your project dependencies.
  2. Step 2: Identify common cause

    This usually happens if the package is not installed via npm or yarn.
  3. Final Answer:

    You forgot to install the @angular/router package -> Option D
  4. Quick Check:

    Missing package causes module not found error [OK]
Hint: Check if package is installed when module not found [OK]
Common Mistakes:
  • Assuming import syntax is wrong
  • Importing from wrong package
  • Ignoring installation step
5. You want to import two Angular features, NgIf and NgFor, from @angular/common in a single import statement. Which is the correct way to do this?
hard
A. import { NgIf, NgFor } from '@angular/common';
B. import NgIf, NgFor from '@angular/common';
C. import { NgIf } and { NgFor } from '@angular/common';
D. import * as NgIf, NgFor from '@angular/common';

Solution

  1. Step 1: Recall syntax for multiple named imports

    Use one import statement with curly braces listing all names separated by commas.
  2. Step 2: Check options for correct syntax

    import { NgIf, NgFor } from '@angular/common'; correctly imports both NgIf and NgFor in one statement with braces and commas.
  3. Final Answer:

    import { NgIf, NgFor } from '@angular/common'; -> Option A
  4. Quick Check:

    Multiple named imports use commas inside braces [OK]
Hint: List multiple imports inside one pair of braces separated by commas [OK]
Common Mistakes:
  • Using multiple import statements unnecessarily
  • Omitting commas between imports
  • Using incorrect keywords like 'and' or '*'