HTTP Client (Java 11+) — Concept Check
Questions on HTTP Client (Java 11+). Java 11's built-in HttpClient provides a modern, async-capable HTTP/1.1 and HTTP/2 client without needing external libraries.
Questions in this quiz
8 questions on HTTP Client (Java 11+) — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Java application using HttpClient to make synchronous requests. The application is experiencing high latency. What could be a potential reason?
- AHttpRequest is not immutable.
- BHttpRequest does not support HTTP/2.
- CHttpClient is not thread-safe.
- DHttpClient is not reused across requests.
2.A developer uses HttpClient to send a POST request with JSON but receives a 415 Unsupported Media Type error. What might be the issue?
- AThe Content-Type header is missing or incorrect.
- BThe URI is incorrect.
- CThe JSON body is malformed.
- DThe HTTP method should be GET.
3.When using HttpClient's sendAsync() method, what is a potential pitfall to be aware of?
- AException handling is not possible with sendAsync().
- BThe response processing must handle CompletableFuture.
- CThe calling thread is blocked until completion.
- DHttpRequest cannot be reused for async requests.
4.Which of the following is true about HttpClient's synchronous send() method?
- AIt blocks the calling thread until the response is received.
- BIt does not support response body handlers.
- CIt requires a separate instance for each request.
- DIt can only handle HTTP/1.1 requests.
5.A developer wants to make multiple HTTP requests in parallel using HttpClient. Which approach should they use?
- AUse a single HttpRequest for all requests.
- BUse multiple threads with synchronous send().
- CUse sendAsync() with CompletableFuture.allOf().
- DCreate a new HttpClient for each request.
6.What happens if you try to reuse an HttpRequest object for a different URI?
- AThe URI will automatically update to the new value.
- BThe request will fail because HttpRequest is immutable.
- CThe request will succeed with the original URI.
- DThe HttpClient will throw an exception.
7.A developer is using HttpClient to make a request but forgets to set a timeout. What could be a consequence?
- AThe request will fail immediately.
- BThe request will hang indefinitely if the server is unresponsive.
- CHttpClient will set a default timeout of 30 seconds.
- DThe request will automatically retry until successful.
8.What is a key advantage of using HttpClient over HttpURLConnection in Java?
- AHttpURLConnection supports WebSockets.
- BHttpClient supports HTTP/2 and asynchronous requests.
- CHttpURLConnection is more modern and efficient.
- DHttpClient is less verbose but less powerful.