0
0
Angularframework~20 mins

GET requests in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular GET Requests Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Angular component display after a successful GET request?

Consider this Angular standalone component that fetches user data on initialization.

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `<div>User: {{ user?.name }}</div>`
})
export class UserComponent implements OnInit {
  user: any;
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http.get('https://api.example.com/user/1').subscribe(data => {
      this.user = data;
    });
  }
}

Assuming the GET request returns {"name": "Alice"}, what will be shown in the browser?

Angular
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `<div>User: {{ user?.name }}</div>`
})
export class UserComponent implements OnInit {
  user: any;
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http.get('https://api.example.com/user/1').subscribe(data => {
      this.user = data;
    });
  }
}
AUser: [object Object]
BUser: undefined
CUser: null
DUser: Alice
Attempts:
2 left
💡 Hint

Look at how the template uses the safe navigation operator ?. and what the GET request returns.

📝 Syntax
intermediate
1:30remaining
Which option correctly performs a GET request using Angular's HttpClient?

Choose the code snippet that correctly uses Angular's HttpClient to perform a GET request and subscribe to the response.

Athis.http.get('url').then(response => console.log(response));
Bthis.http.get('url').on('response', res => console.log(res));
Cthis.http.get('url').subscribe(response => console.log(response));
Dthis.http.get('url').fetch(response => console.log(response));
Attempts:
2 left
💡 Hint

Remember that Angular's HttpClient returns an Observable, not a Promise.

🔧 Debug
advanced
2:30remaining
Why does this Angular GET request fail with a CORS error?

Look at this Angular service method:

getData() {
  return this.http.get('https://external-api.com/data');
}

When called, the browser console shows a CORS error. What is the most likely cause?

Angular
getData() {
  return this.http.get('https://external-api.com/data');
}
AThe external API does not allow cross-origin requests from your app's domain.
BThe URL is missing the HTTP protocol prefix.
CAngular HttpClient requires POST method for external APIs.
DThe HttpClient module is not imported in the app module.
Attempts:
2 left
💡 Hint

CORS errors happen when the server blocks requests from different origins.

state_output
advanced
2:00remaining
What is the value of 'data' after this GET request completes?

Given this Angular component code:

data: any;

constructor(private http: HttpClient) {}

ngOnInit() {
  this.http.get('https://api.example.com/items').subscribe(response => {
    this.data = response;
  });
}

If the API returns [{"id":1,"name":"Item1"},{"id":2,"name":"Item2"}], what will data contain?

Angular
data: any;

constructor(private http: HttpClient) {}

ngOnInit() {
  this.http.get('https://api.example.com/items').subscribe(response => {
    this.data = response;
  });
}
A{"id":1,"name":"Item1"}
B[{"id":1,"name":"Item1"},{"id":2,"name":"Item2"}]
Cundefined
Dnull
Attempts:
2 left
💡 Hint

The API returns an array of objects. The subscription assigns the whole response to data.

🧠 Conceptual
expert
3:00remaining
Which statement best explains Angular's HttpClient GET request behavior?

Choose the statement that correctly describes how Angular's HttpClient GET requests work.

AHttpClient GET returns an Observable that emits the response once and completes automatically.
BHttpClient GET returns a Promise that resolves with the response data.
CHttpClient GET returns an Observable that keeps emitting data every second until unsubscribed.
DHttpClient GET returns a synchronous response immediately without waiting.
Attempts:
2 left
💡 Hint

Think about how Angular handles HTTP requests and Observables.