Question 3Medium
Why does this call usually need subscribe()?
this.http.get('/api/items')
- The request is sent immediately and subscribe only logs
- HttpClient requests are cold observables that run only when subscribed
- subscribe converts JSON to objects
- subscribe is required by the router
Answer: B. HttpClient requests are cold observables that run only when subscribed
Explanation
Without subscribing (or async pipe) no request is sent.
Step-by-step solution
- Cold observable.
Why the other options are wrong
- A. HttpClient requests do not run until subscribed, so nothing is sent.
- C. JSON parsing is automatic.
- D. The router does not need subscribe; HTTP requests need it to run.
Question 4Medium
What is an HTTP interceptor used for?
- Routing browser requests to the matching component and managing the browser history stack
- Compiling templates ahead of time
- Inspecting or modifying requests and responses globally, such as adding an auth header
- Caching only the image responses
Answer: C. Inspecting or modifying requests and responses globally, such as adding an auth header
Explanation
Interceptors form a chain around HttpClient.
Step-by-step solution
- Cross-cutting concerns.
Why the other options are wrong
- A. Interceptors do not route requests or manage browser history.
- B. Interceptors handle HTTP traffic, not template compilation.
- D. Interceptors act on all HTTP requests, not only image responses.
Question 5Hard
An interceptor must call next.handle(req) (or next(req) in functional style) because:
- Otherwise the request never continues to the backend
- It logs the request automatically to the browser console
- It unsubscribes the request
- It clones the request for caching
Answer: A. Otherwise the request never continues to the backend
Explanation
The chain proceeds only when passed along.
Step-by-step solution
- Interceptors form a chain.
Why the other options are wrong
- B. Calling next does not log requests to the console automatically.
- C. Calling next does not unsubscribe the request.
- D. Calling next does not clone the request for caching.
Question 6Hard
Why does HttpRequest need clone() to add a header in an interceptor?
- Clones are faster
- Headers are read-only on the server
- Requests are immutable, so you create a modified copy
- Angular forbids headers
Answer: C. Requests are immutable, so you create a modified copy
Explanation
req.clone({setHeaders: ...}).
Step-by-step solution
- Immutable request.
Why the other options are wrong
- A. Cloning is needed for immutability, not because it is faster.
- B. Headers are not read-only on the server; the request is immutable.
- D. Angular does not forbid headers; requests are just immutable.