AlgoMaster Logo

Networking - Quiz

Last Updated: December 6, 2025

1 min read

Networking Exercises

31 quizzes

1
Code Completion

Resolve a hostname to its IP address so you can print it.

java
1
InetAddress address = InetAddress.("example.com");

Click an option to fill the blank:

2
Code Completion

Create a URL object pointing to an HTTP resource so you can later open a connection.

java
1
URL apiUrl = new URL("http", "api.example.com", 80, "/status");

Click an option to fill the blank:

3
Code Completion

Send an HTTP GET request asynchronously using the Java 11 HTTP client.

java
1
client.(request, HttpResponse.BodyHandlers.ofString());

Click an option to fill the blank:

4
Multiple Choice

Which OSI layer is primarily responsible for routing packets between different networks?

5
Multiple Choice

For a video streaming application where occasional packet loss is acceptable, which transport protocol is usually preferred?

6
Multiple Choice

Which method would you use to obtain the local machine's IP and hostname using InetAddress?

7
Multiple Choice

Given URL url = new URL("https://example.com:8443/app/status?ok=true#top"); what does url.getPort() return?

8
Multiple Choice

Which URLConnection method gives you a stream to read data from the remote resource?

9
Multiple Choice

In a TCP client using java.net.Socket, which operation actually initiates the connection to the server?

10
Multiple Choice

A Java ServerSocket is typically used to:

11
Multiple Choice

Which class is used with DatagramSocket to represent a UDP message in Java?

12
Multiple Choice

Which Java 11 class provides a modern, built-in HTTP client supporting HTTP/2?

13
Multiple Choice

In the OSI model, which layer typically carries protocols like HTTP and FTP?

14
Multiple Choice

When opening a URLConnection, which property should you set to avoid waiting indefinitely on slow servers?

15
Sequencing

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.

16
Sequencing

Order the steps to accept a TCP client connection and send a greeting using ServerSocket.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

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}
18
Output Prediction

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}
19
Output Prediction

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}
20
Output Prediction

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}
21
Output Prediction

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}
22
Bug Spotting

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.

java
1
import java.io.PrintWriter;
2
import java.net.ServerSocket;
3
import java.net.Socket;
4
 
5
public class GreetingServer {
6
    public static void main(String[] args) throws Exception {
7
        ServerSocket serverSocket = new ServerSocket(9000);
8
        Socket client = serverSocket.accept();
9
        PrintWriter out = new PrintWriter(client.getOutputStream());
10
        out.println("Hello client");
11
        serverSocket.close();
12
    }
13
}
23
Bug Spotting

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.

java
1
import java.net.DatagramPacket;
2
import java.net.DatagramSocket;
3
 
4
public class UdpSender {
5
    public static void main(String[] args) throws Exception {
6
        byte[] data = "hello".getBytes("UTF-8");
7
        DatagramPacket packet = new DatagramPacket(data, data.length);
8
        DatagramSocket socket = new DatagramSocket();
9
        socket.send();
10
        socket.close();
11
    }
12
}
24
Matching

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.

25
Matching

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.

26
Fill in the Blanks

Complete the code to open a URLConnection and set a read timeout before reading.

java
1
import java.io.BufferedReader;
2
import java.io.InputStreamReader;
3
import java.net.URL;
4
import java.net.URLConnection;
5
6
public class ReadWithTimeout {
7
public static void main(String[] args) throws Exception {
8
URL url = new URL("https://example.com");
9
URLConnection conn = url.();
10
conn.(5000);
11
BufferedReader in = new BufferedReader(new InputStreamReader(conn.()));
12
String line = in.readLine();
13
System.out.println(line);
14
in.close();
15
}
16
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to create a basic TCP server and accept one client.

java
1
import java.net.ServerSocket;
2
import java.net.Socket;
3
4
public class SingleClientServer {
5
public static void main(String[] args) throws Exception {
6
server = new (5000);
7
client = server.();
8
System.out.println("Client connected: " + client.getInetAddress());
9
client.close();
10
server.close();
11
}
12
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to create an HttpClient and build a GET request to a given URI.

java
1
import java.net.URI;
2
import java.net.http.HttpClient;
3
import java.net.http.HttpRequest;
4
5
public class SimpleGet {
6
public static void main(String[] args) {
7
HttpClient client = HttpClient.();
8
HttpRequest request = HttpRequest.newBuilder()
9
.uri(URI.("https://api.example.com/status"))
10
.();
11
System.out.println("Request created: " + request.uri());
12
}
13
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the UDP server code to receive a packet and print the message.

java
1
import java.net.DatagramPacket;
2
import java.net.DatagramSocket;
3
4
public class UdpServerOnce {
5
public static void main(String[] args) throws Exception {
6
byte[] buffer = new byte[1024];
7
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
8
DatagramSocket socket = new DatagramSocket();
9
socket.(packet);
10
String msg = new String(packet.getData(), 0, packet.());
11
System.out.println(msg);
12
socket.close();
13
}
14
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that will throw an exception if the host name cannot be resolved.

Click on the line to select.

java
1
import java.net.InetAddress;
2
 
3
public class ResolveHost {
4
    public static void main(String[] args) throws Exception {
5
        InetAddress addr = InetAddress.getByName("unknown.host.example");
6
        System.out.println(addr.getHostAddress());
7
    }
8
}
31
Hotspot Selection

Click the line that opens the TCP connection to the remote server.

Click on the line to select.

java
1
import java.io.BufferedReader;
2
import java.io.InputStreamReader;
3
import java.net.Socket;
4
 
5
public class TimeClient {
6
    public static void main(String[] args) throws Exception {
7
        Socket socket = new Socket("time.example.com", 13);
8
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
9
        String line = in.readLine();
10
        System.out.println(line);
11
        socket.close();
12
    }
13
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.