Last Updated: December 6, 2025
31 quizzes
Resolve a hostname to its IP address so you can print it.
InetAddress address = InetAddress.("example.com");Click an option to fill the blank:
Create a URL object pointing to an HTTP resource so you can later open a connection.
URL apiUrl = new URL("http", "api.example.com", 80, "/status");Click an option to fill the blank:
Send an HTTP GET request asynchronously using the Java 11 HTTP client.
client.(request, HttpResponse.BodyHandlers.ofString());Click an option to fill the blank:
Which OSI layer is primarily responsible for routing packets between different networks?
For a video streaming application where occasional packet loss is acceptable, which transport protocol is usually preferred?
Which method would you use to obtain the local machine's IP and hostname using InetAddress?
Given URL url = new URL("https://example.com:8443/app/status?ok=true#top"); what does url.getPort() return?
Which URLConnection method gives you a stream to read data from the remote resource?
In a TCP client using java.net.Socket, which operation actually initiates the connection to the server?
A Java ServerSocket is typically used to:
Which class is used with DatagramSocket to represent a UDP message in Java?
Which Java 11 class provides a modern, built-in HTTP client supporting HTTP/2?
In the OSI model, which layer typically carries protocols like HTTP and FTP?
When opening a URLConnection, which property should you set to avoid waiting indefinitely on slow servers?
Order the steps to send an HTTP GET request with Java 11 HttpClient and print the response body synchronously.
Drag and drop to reorder, or use the arrows.
Order the steps to accept a TCP client connection and send a greeting using ServerSocket.
Drag and drop to reorder, or use the arrows.
What is the output of this code that inspects a URL?
1import java.net.URL;
2
3public class UrlTest {
4 public static void main(String[] args) throws Exception {
5 URL url = new URL("https://example.com:8080/path/data");
6 System.out.println(url.getHost() + ":" + url.getPort());
7 }
8}What is the output of this code that uses InetAddress for the local host?
1import java.net.*;
2
3public class LocalHostTest {
4 public static void main(String[] args) throws Exception {
5 InetAddress local = InetAddress.getLocalHost();
6 System.out.println(local.equals(InetAddress.getByName(local.getHostName())));
7 }
8}What does this TCP client code print if the connection is established successfully?
1import java.net.*;
2
3public class SocketTest {
4 public static void main(String[] args) throws Exception {
5 try (Socket socket = new Socket("example.com", 80)) {
6 System.out.println(socket.isConnected());
7 }
8 }
9}What is the output when sending a synchronous GET request using HttpClient?
1import java.net.URI;
2import java.net.http.*;
3
4public class HttpClientTest {
5 public static void main(String[] args) throws Exception {
6 HttpClient client = HttpClient.newHttpClient();
7 HttpRequest request = HttpRequest.newBuilder()
8 .uri(URI.create("https://www.example.com"))
9 .build();
10 HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
11 System.out.println(response.statusCode());
12 }
13}What does this UDP code print about the received packet length?
1import java.net.*;
2
3public class PacketLengthTest {
4 public static void main(String[] args) throws Exception {
5 byte[] data = "ping".getBytes("UTF-8");
6 DatagramPacket packet = new DatagramPacket(data, data.length);
7 System.out.println(packet.getLength());
8 }
9}This server code is intended to accept a client and send a greeting once, but it has a networking issue. Identify the bug.
Click on the line(s) that contain the bug.
import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket; public class GreetingServer { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(9000); Socket client = serverSocket.accept(); PrintWriter out = new PrintWriter(client.getOutputStream()); out.println("Hello client"); serverSocket.close(); }}This code tries to send a UDP message, but it has an error. Find and fix it.
Click on the line(s) that contain the bug.
import java.net.DatagramPacket;import java.net.DatagramSocket; public class UdpSender { public static void main(String[] args) throws Exception { byte[] data = "hello".getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(data, data.length); DatagramSocket socket = new DatagramSocket(); socket.send(); socket.close(); }}Match each networking class with its primary purpose.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the HTTP client components with what they represent.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to open a URLConnection and set a read timeout before reading.
import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;public class ReadWithTimeout { public static void main(String[] args) throws Exception { URL url = new URL("https://example.com"); URLConnection conn = url.(); conn.(5000); BufferedReader in = new BufferedReader(new InputStreamReader(conn.())); String line = in.readLine(); System.out.println(line); in.close(); }}Click an option to fill blank 1:
Complete the code to create a basic TCP server and accept one client.
import java.net.ServerSocket;import java.net.Socket;public class SingleClientServer { public static void main(String[] args) throws Exception { server = new (5000); client = server.(); System.out.println("Client connected: " + client.getInetAddress()); client.close(); server.close(); }}Click an option to fill blank 1:
Complete the code to create an HttpClient and build a GET request to a given URI.
import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;public class SimpleGet { public static void main(String[] args) { HttpClient client = HttpClient.(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.("https://api.example.com/status")) .(); System.out.println("Request created: " + request.uri()); }}Click an option to fill blank 1:
Complete the UDP server code to receive a packet and print the message.
import java.net.DatagramPacket;import java.net.DatagramSocket;public class UdpServerOnce { public static void main(String[] args) throws Exception { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramSocket socket = new DatagramSocket(); socket.(packet); String msg = new String(packet.getData(), 0, packet.()); System.out.println(msg); socket.close(); }}Click an option to fill blank 1:
Click the line that will throw an exception if the host name cannot be resolved.
Click on the line to select.
import java.net.InetAddress; public class ResolveHost { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getByName("unknown.host.example"); System.out.println(addr.getHostAddress()); }}Click the line that opens the TCP connection to the remote server.
Click on the line to select.
import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.Socket; public class TimeClient { public static void main(String[] args) throws Exception { Socket socket = new Socket("time.example.com", 13); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = in.readLine(); System.out.println(line); socket.close(); }}