Java Networking — Practice MCQ Exam
Based on Lecture 9: Networking (Sockets, Streams, Connection Lifecycle)
Instructions: Choose the single best answer for each question. Answers are listed at the end of
the document — try to complete all questions before checking.
1. At which layer of the network stack does all Java networking communication (via
[Link]) occur?
A. Physical Layer
B. Data Link Layer
C. Network/IP Layer
D. Application Layer
2. What is the primary benefit of the [Link] package, according to the lecture?
A. It encrypts all network traffic automatically
B. It abstracts away low-level physical routing so developers can focus on endpoints and
data streams
C. It replaces the need for IP addresses
D. It only works with wireless networks
3. Which statement correctly distinguishes TCP from UDP?
A. TCP is connectionless; UDP is connection-oriented
B. TCP guarantees ordered, reliable delivery; UDP is unreliable and unordered
C. TCP is faster than UDP because it skips acknowledgments
D. UDP requires a three-step handshake; TCP does not
4. Which pair of Java classes belongs to TCP communication in [Link]?
A. DatagramSocket and DatagramPacket
B. Socket and ServerSocket
C. URL and URLConnection
D. InetAddress and MulticastSocket
5. A developer is building a live video conferencing feature where occasional
dropped frames are acceptable but speed is critical. Which protocol and Java class
should they use?
A. TCP using Socket
B. UDP using DatagramSocket
C. TCP using ServerSocket
D. UDP using URLConnection
6. In the "IP address as building, port as door" analogy used in the lecture, what does
the port number identify?
A. The physical hardware device
B. The specific application/process on a device that should receive the data
C. The geographic location of the server
D. The encryption key for the connection
7. What is the valid range of port numbers in TCP/IP networking?
A. 0 to 1023
B. 0 to 65535
C. 1 to 9999
D. 1024 to 49151
8. Ports 0 through 1023 are classified as:
A. Ephemeral ports
B. Well-known/reserved ports
C. Private ports
D. Multicast-only ports
9. Which class is responsible for listening for and accepting incoming client
connection requests on the server side of a TCP connection?
A. Socket
B. ServerSocket
C. DatagramSocket
D. InetAddress
10. Which class represents the actual two-way TCP connection endpoint used to
send and receive data?
A. ServerSocket
B. Socket
C. DatagramPacket
D. URL
11. Which class would you use to send and receive individual UDP packets?
A. Socket
B. ServerSocket
C. DatagramSocket
D. MulticastSocket only
12. Group communication to multiple recipients over UDP is best handled using
which class?
A. DatagramSocket
B. MulticastSocket
C. ServerSocket
D. URLConnection
13. According to the "Socket Endpoint" rules in the lecture, what must happen if a
client needs to communicate with a new, different remote host?
A. The existing socket is reused automatically
B. A new socket must be created for the new host
C. The port number must be set to 0
D. The ServerSocket must be restarted
14. What is the correct first step in establishing a TCP server, based on the
handshake flowchart?
A. Socket s = new Socket(localhost, 8000);
B. ServerSocket server = new ServerSocket(8000);
C. [Link]();
D. [Link]();
15. Which line of server code blocks execution until a client makes a connection
request?
A. ServerSocket server = new ServerSocket(8000);
B. Socket socket = [Link]();
C. [Link]();
D. [Link]();
16. A client creates a socket like this: new Socket("[Link]", 8000). If the
JVM cannot resolve this hostname via DNS, which exception is thrown?
A. BindException
B. ConnectException
C. UnknownHostException
D. EOFException
17. Which hostname or IP address refers to the same machine a program is running
on?
A. [Link] or "remote"
B. localhost or [Link]
C. [Link] or "broadcast"
D. [Link] or "any"
18. Why does the lecture recommend wrapping a socket's raw
InputStream/OutputStream in DataInputStream/DataOutputStream?
A. The raw stream classes cannot be used over a network at all
B. DataInputStream/DataOutputStream allow direct, convenient transmission of Java
primitive types like int, double, and String
C. They automatically compress the data being sent
D. They are required for UDP but not TCP
19. After a server's [Link]() call returns, what exactly does it return?
A. The original ServerSocket, now connected
B. A new Socket, bound to the same local port, with its remote endpoint set to the client's
address and port
C. A DatagramPacket containing the client's data
D. Nothing; it only unblocks the loop
20. What is the purpose of calling flush() on a DataOutputStream after a write
operation?
A. It closes the connection
B. It empties the input stream's buffer
C. It forces any buffered data to actually be transmitted over the network immediately
D. It converts the TCP stream into a UDP datagram
21. In the client/server circle-area example from the lecture, what data does the client
send to the server?
A. The computed area of the circle
B. The radius of the circle
C. A serialized Circle object
D. A text file containing measurements
22. A server tries to start with new ServerSocket(8000), but another running
application is already using port 8000. Which exception results?
A. ConnectException
B. BindException
C. UnknownHostException
D. EOFException
23. A client attempts new Socket(host, port), but the server process is not running on
that host/port. Which exception is most likely thrown?
A. BindException
B. ConnectException (connection refused)
C. EOFException
D. UnknownHostException
24. While a program is actively reading from a stream, the connection on the other
end closes unexpectedly mid-read. Which exception is most associated with this
scenario?
A. BindException
B. ConnectException
C. [Link]
D. UnknownHostException
25. In the multithreaded server architecture, what does the main server loop do
immediately after [Link]() returns a new socket?
A. It processes the entire client request itself before accepting the next connection
B. It spawns a new thread (or submits to a thread pool) to handle that client's I/O, then
immediately loops back to accept() again
C. It closes the ServerSocket
D. It converts the socket into a DatagramSocket
26. Why is a basic single-threaded server considered a bottleneck when serving
multiple clients?
A. It can only use UDP, not TCP
B. The blocking accept()/processing call handles one client fully before it can accept the
next, forcing others to wait in a queue
C. It cannot bind to a port number above 1023
D. It requires a separate ServerSocket per client
27. Before a Java object can be sent across the network using
[Link](), its class must implement which interface?
A. Runnable
B. Comparable
C. Serializable
D. Cloneable
28. Which class is used to parse a string like
"[Link] into its protocol, host, port, and path
components?
A. InetAddress
B. URL
C. Socket
D. DatagramSocket
Answer Key
1. D 2. B 3. B 4. B
5. B 6. B 7. B 8. B
9. B 10. B 11. C 12. B
13. B 14. B 15. B 16. C
17. B 18. B 19. B 20. C
21. B 22. B 23. B 24. C
25. B 26. B 27. C 28. B
Tip: For any question you missed, go back to the corresponding slide/section in Lecture 9 and
re-read the explanation before moving on.