Chapter 35 Self-Assessment Quiz: Networking and Internet Programming

Test your understanding of networking concepts and Pascal network programming. Try to answer each question before revealing the answer.


Section 1: Multiple Choice

Q1. In the TCP/IP model, HTTP operates at which layer?

(a) Link layer (b) Internet layer (c) Transport layer (d) Application layer

Answer (d) HTTP is an application-layer protocol. It sits on top of TCP (transport layer), which sits on top of IP (internet layer).

Q2. Which HTTP method is used to create a new resource in a REST API?

(a) GET (b) POST (c) PUT (d) DELETE

Answer (b) POST is the standard HTTP method for creating new resources. GET retrieves, PUT updates (or creates at a specific URL), and DELETE removes.

Q3. What HTTP status code indicates "resource not found"?

(a) 200 (b) 301 (c) 404 (d) 500

Answer (c) 404 means the requested resource was not found. 200 = success, 301 = permanent redirect, 500 = internal server error.

Q4. In MicroServe, what separates the HTTP headers from the body?

(a) A semicolon (b) A blank line (\r\n\r\n) (c) The Content-Length header (d) A special END-HEADERS marker

Answer (b) In HTTP, headers and body are separated by a blank line (CRLF CRLF). This is a fundamental part of the HTTP protocol specification.

Q5. Which Free Pascal unit provides the TFPHTTPClient class?

(a) ssockets (b) fphttpclient (c) Sockets (d) httpprotocol

Answer (b) TFPHTTPClient is in the fphttpclient unit. ssockets provides raw TCP socket classes. Sockets provides lower-level socket functions.

Q6. Why should network operations always be wrapped in try-except blocks?

(a) The compiler requires it (b) Network operations can fail for reasons outside the program's control (c) It makes the code faster (d) HTTP requires exception handling

Answer (b) Network operations can fail due to server downtime, DNS resolution failure, timeout, connection reset, or network outage — all beyond your control. Without exception handling, any of these failures would crash the program.

Q7. What is the default behavior of a simple TCP server that handles clients in the OnConnect callback without threading?

(a) It handles all clients simultaneously (b) It handles one client at a time (blocking) (c) It drops excess connections (d) It queues connections and handles them in random order

Answer (b) A simple server without threading handles one client at a time. While serving one client, all other clients must wait. This is called blocking I/O. For concurrent handling, you need threading (Chapter 36) or asynchronous I/O.

Q8. WebSockets differ from HTTP primarily in that:

(a) WebSockets use UDP instead of TCP (b) WebSockets provide persistent, full-duplex communication (c) WebSockets are faster than HTTP for all use cases (d) WebSockets do not require a server

Answer (b) WebSockets provide a persistent, full-duplex connection where both sides can send messages at any time. HTTP is request-response (client asks, server answers). WebSockets still use TCP, and they are not necessarily faster for request-response patterns.

Section 2: True/False

Q9. A TCP server can only serve clients on the same machine (localhost).

Answer False. A TCP server listening on "0.0.0.0" accepts connections from any network interface, including remote machines. Listening on "127.0.0.1" (localhost) restricts to local connections only. The choice depends on security requirements.

Q10. The Content-Type: application/json header tells the server that the request body contains JSON data.

Answer True. The Content-Type header specifies the media type of the request or response body. Setting it to "application/json" tells the recipient to parse the body as JSON.

Q11. HTTP/1.1 connections are closed after every request by default.

Answer False. HTTP/1.1 defaults to keep-alive connections — the connection stays open for multiple requests. HTTP/1.0 closes after each request by default. This is why MicroServe explicitly sets Connection: close.

Section 3: Short Answer

Q12. Explain why MicroServe uses a router (mapping paths to handler functions) rather than a single large if-else chain.

Answer A router separates the routing logic from the handler logic, making both easier to maintain. Adding a new route requires only registering a handler function — no modification of existing code. A large if-else chain grows unwieldy as routes increase, mixes routing with handling, and violates the open/closed principle. The router pattern also makes it easy to add middleware (logging, authentication) that applies to all routes.

Q13. What three things must a REST client do when calling a POST /api/expenses endpoint? Describe each step.

Answer (1) Construct the request body as JSON (create a TJSONObject, add fields for description, amount, category, etc., convert to string). (2) Set the appropriate headers: Content-Type: application/json (tells the server the body format), Accept: application/json (tells the server what format we want back). (3) Send the POST request and handle the response: check the status code (201 = created, 400 = validation error, 500 = server error), parse the response body if needed, handle network exceptions.

Q14. A student writes a network client that works perfectly when tested locally but fails when connecting to a remote server. List three possible causes and how to diagnose each.

Answer (1) Firewall blocking the connection — diagnose by trying to connect with a known tool (telnet, curl). (2) DNS resolution failure — diagnose by using the IP address directly instead of the hostname. (3) Timeout too short — remote servers have higher latency than localhost; increase IOTimeout and ConnectTimeout. Other valid answers: server only listening on localhost (not 0.0.0.0), wrong port, SSL/TLS required but client using plain HTTP.