🎓 BookMCQ
← Back to 25. Introduction to Application Layer

📝 Addresses and Ports in Java (22 MCQs)

Representing Network Endpoints in Java Programs

Java represents network destinations with host addresses and transport port numbers. InetAddress represents an IP address and supports name resolution, while InetSocketAddress combines an address or host name with a port. Socket and DatagramSocket use these endpoint values to establish TCP communication or exchange UDP datagrams. Together, these values identify where an application should direct its traffic for each network request.

InetAddress, InetSocketAddress and Port Values

InetAddress can represent IPv4 or IPv6 addresses, and methods such as getByName may resolve a host name. InetSocketAddress pairs an address with a port in the range 0 through 65535. Port zero is commonly used for automatic local port assignment, not as a normal remote service destination. A port is an integer endpoint value, not part of the IP address.

Resolving a Host and Selecting Its Service Port

A Java client resolves a server name to an InetAddress, creates an InetSocketAddress with the intended service port, and passes that endpoint to a Socket constructor or connect method. The operating system selects a local ephemeral port when needed. The server's destination port identifies the requested service on the remote host.

Connecting to a Specific IPv4 Service

A Java client can call InetAddress.getByName with 192.0.2.45 and create an InetSocketAddress using port 8080. Passing that endpoint to Socket.connect initiates a TCP connection. The remote address identifies the host, while port 8080 selects the application endpoint listening there. The operating system chooses the client's local source port during connection setup.

Address Resolution and Transport Demultiplexing

Host names make applications independent of fixed numeric addresses, but DNS resolution must produce an address before ordinary IP communication. Port numbers then distinguish application endpoints on the host. TCP and UDP maintain separate transport contexts, so an identical numeric port can be used by both protocols. This is how one host can run several network services simultaneously.

Address and Port Misconceptions in Java

'An InetAddress contains a port' is false because the port belongs in an endpoint structure such as InetSocketAddress. 'A port identifies a computer' confuses the transport endpoint with the host address. 'Port zero means the remote service is unknown' ignores its common use for local ephemeral-port assignment. The protocol type must also be considered when describing a port endpoint.

Interpreting Java Endpoint Objects and Ranges

For examinations, distinguish host names, IP addresses, InetAddress, InetSocketAddress, and port numbers. Check whether a port is valid, identify when the operating system assigns an ephemeral local port, and trace name resolution before connection. Explain how destination address and port together identify the remote service endpoint. Be precise about whether an address is local, remote, numeric, or resolved.

Java Network Addressing Terms

  • InetAddress - Java representation of an IP address.
  • InetSocketAddress - Combines an IP address with a port.
  • Port number - Identifies a transport service endpoint.
  • Ephemeral port - Temporary local port assigned by the system.
  • DNS resolution - Maps a host name to address records.
  • Endpoint - Address and port identifying a communication peer.
7
Easy
10
Medium
5
Hard

📝 All Addresses and Ports in Java MCQs

Q1. A server program binds to port 0 instead of a specific port number. What is the most likely effect of this action?

A.The server will listen on all ports simultaneously.
B.The binding will fail with an error.
C.The operating system will assign a random available port. ✅
D.The server will only accept connections from localhost.
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: When a program binds to port 0, the operating system treats it as a request for an unused ephemeral port. It chooses a free port from the dynamic range (typically 49152‑65535) and assigns it to the socket. This allows the server to start without a predetermined port, which is why option C is correct.

Q2. A NAT device maps internal address 192.168.1.10:12345 to external address 203.0.113.5:54321. Which statement must be true?

A.The internal host can still be reached using its private address.
B.The external address will be used for all other internal hosts. ✅
C.The external port 54321 uniquely identifies this internal session.
D.The NAT will change the IP but keep the same port number.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Network address translation creates a one‑to‑one mapping between an internal socket (IP + port) and an external socket. The external port number (54321) is allocated specifically for the internal session 192.168.1.10:12345, so any packet arriving at 203.0.113.5:54321 is forwarded to the internal host. Hence option B is correct.

Q3. If a firewall blocks all inbound traffic to ports 1024–49151, which of the following services could still be reachable from the Internet?

A.HTTP (port 80) ✅
B.SSH (port 22)
C.MySQL (port 3306)
D.SMTP (port 25)
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The blocked range covers only the registered ports (1024‑49151). Well‑known ports (0‑1023) remain unaffected. HTTP uses port 80, which lies outside the blocked interval, allowing it to be reachable. The other listed services use ports within the blocked range, so they would be denied. Therefore option A is correct.

Q4. Which statement best distinguishes well‑known ports from registered ports?

A.Well‑known ports are assigned by IANA and range from 0 to 1023, while registered ports range from 1024 to 49151. ✅
B.Well‑known ports are used only for UDP, whereas registered ports are used only for TCP.
C.Well‑known ports can be changed by any application, but registered ports require administrator privileges.
D.Well‑known ports are dynamically allocated, while registered ports are static.
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: IANA defines the classification: ports 0‑1023 are called well‑known and are reserved for core services (e.g., HTTP, DNS). Ports 1024‑49151 are registered for less‑common applications and can be allocated by developers. The other statements mix protocols or misuse the terms, making option A the accurate distinction.

Q5. What is the primary risk of assigning the same port number to multiple services on a single host?

A.Increased bandwidth utilization
B.Port‑number collisions causing connection failures
C.Reduced latency for all services
D.Simplified firewall configuration ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: When two applications listen on the same port, the operating system cannot differentiate which process should handle incoming packets, leading to ambiguous routing and possible connection failures. This conflict undermines reliability and security, making option D the most appropriate description of the risk.

Q6. An application uses UDP port 53 for DNS queries while another service on the same host uses TCP port 53 for DNS zone transfers. How does the OS differentiate the traffic?

A.By inspecting the first byte of the payload
B.By the transport‑layer protocol field in the IP header ✅
C.By the source IP address only
D.By the packet’s TTL value
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The IP header contains a protocol field that indicates whether the payload is TCP (protocol 6) or UDP (protocol 17). Even though both services share port 53, the OS uses this field to separate the two streams. Therefore option B correctly explains the differentiation mechanism.

Q7. In a typical client‑server interaction, what role does the client’s transient port play?

A.It identifies the server’s application.
B.It determines the routing path of packets.
C.It uniquely pairs the client socket with the server socket. ✅
D.It encrypts the data payload.
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: When a client initiates a connection, the OS assigns an ephemeral (transient) port from the dynamic range. Together with the client’s IP address, this port forms a unique socket identifier that pairs with the server’s well‑known port, enabling the server to send responses back to the correct client process. Hence option C is correct.

Q8. How does the combination of an IP address and a port number uniquely identify a communication endpoint?

A.The IP address alone is sufficient; the port is only for logging. ✅
B.The port number alone distinguishes endpoints; the IP address is optional.
C.The tuple (source IP, source port, destination IP, destination port) defines a unique socket.
D.Only the destination port matters for uniqueness.
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: A socket is uniquely identified by the four‑tuple comprising source IP, source port, destination IP, and destination port. This combination ensures that multiple connections can coexist on the same host without ambiguity. Option A captures this principle, emphasizing that the port works together with the IP address to form a unique endpoint.

Q9. During the TCP three‑way handshake, how does dynamic port allocation affect the client’s SYN packet?

A.The client must use a well‑known port for the SYN.
B.The client’s OS selects an unused transient port for the SYN’s source field.
C.The SYN packet always carries port 0 as the source.
D.The server chooses the client’s source port in the SYN‑ACK. ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: When a client initiates a connection, it sends a SYN with a source port chosen from the dynamic range. The server responds with a SYN‑ACK that includes the same source port (the server’s well‑known port) and acknowledges the client’s transient port. This process allows the server to direct subsequent packets correctly. Thus option D correctly describes the role of dynamic port allocation.

Q10. What is the effect of binding a server socket to 0.0.0.0 versus 127.0.0.1?

A.0.0.0.0 limits connections to the local machine only, while 127.0.0.1 accepts connections from any network.
B.0.0.0.0 makes the service reachable on all local interfaces, whereas 127.0.0.1 restricts it to the loopback interface. ✅
C.Both addresses behave identically; they are just different notations for the same interface.
D.Binding to 0.0.0.0 disables the socket, while 127.0.0.1 enables it.
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The wildcard address 0.0.0.0 tells the OS to listen on every network interface the host possesses, making the service externally reachable. In contrast, 127.0.0.1 is the loopback address, confining connections to processes on the same machine. Therefore option B correctly contrasts the two binding choices.

Q11. What security implication arises from exposing a public service on a high‑numbered (ephemeral) port?

A.It automatically encrypts traffic.
B.It reduces the chance of automated scans discovering the service.
C.It guarantees compliance with PCI‑DSS standards. ✅
D.It eliminates the need for firewall rules.
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Using a high‑numbered port does not provide intrinsic security; attackers can still scan the entire port range. While obscurity may slightly lower the probability of casual discovery, robust security still requires proper authentication, encryption, and firewall policies. Hence option C, which incorrectly claims compliance, is the accurate assessment of the implication.

Q12. When the socket option SO_REUSEADDR is enabled, what behavior does it permit?

A.Multiple sockets can bind to the same IP and port simultaneously.
B.A socket can be bound to a port that is still in the TIME_WAIT state. ✅
C.The socket will automatically reuse the same local IP address for all connections.
D.The socket ignores all firewall rules for that port.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: SO_REUSEADDR allows a new socket to bind to a port that a previous socket has left in the TIME_WAIT state, facilitating rapid restarts of services. It does not allow simultaneous bindings or bypass firewall policies. Therefore option B correctly describes the effect of enabling this option.

Q13. Which of the following best defines a well‑known port?

A.A port number between 0 and 1023 assigned by IANA for core services. ✅
B.A port number that can be changed by any user at runtime.
C.A port number reserved for experimental protocols only.
D.A port number that is automatically selected by the OS for each new connection.
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: IANA reserves the range 0‑1023 for well‑known ports, assigning them to widely used services such as HTTP (80) and DNS (53). These ports are globally recognized and typically require privileged access to bind. The other statements mischaracterize the purpose of well‑known ports, making option A the correct definition.

Q14. Which IANA‑defined range is allocated for dynamic or private ports?

A.0‑1023 (well‑known)
B.1024‑49151 (registered)
C.49152‑65535 (dynamic/private) ✅
D.65536‑70000 (experimental)
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: IANA designates the range 49152‑65535 for dynamic or private ports, also called the ephemeral range. These ports are assigned temporarily by the operating system for client‑side connections. The answer choice D correctly identifies this range, while the other ranges correspond to well‑known or registered ports.

Q15. A network scanner reports that ports 22, 80, and 443 are closed, but port 8080 is open. What inference can be drawn about the host’s configuration?

A.The host runs a web server on the standard HTTP port.
B.The host only allows inbound traffic on non‑standard ports. ✅
C.The host has no services running at all.
D.The host blocks all traffic regardless of port.
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Port 8080 is commonly used as an alternative HTTP port. Since standard ports (22, 80, 443) are closed while 8080 is open, the host appears to permit inbound connections only on a non‑standard port, likely as a security‑through‑obscurity measure. Hence option B is the logical inference.

Q16. How does PAT (Port Address Translation) differ from static NAT regarding port mapping?

A.PAT maps many internal addresses to a single external address using unique ports, while static NAT maps one‑to‑one without altering ports. ✅
B.Static NAT changes the source port, whereas PAT keeps the original port unchanged.
C.Both techniques always preserve the original port numbers.
D.PAT only works with UDP, while static NAT only works with TCP.
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: PAT, also called NAT overload, translates multiple private IPs to one public IP by assigning distinct external ports for each internal session. Static NAT, by contrast, provides a fixed 1:1 mapping and does not modify port numbers. Option A accurately captures this distinction.

Q17. In the socket tuple (source IP, source port, destination IP, destination port), which element guarantees that two different applications on the same host can communicate simultaneously with the same remote server?

A.The source IP address
B.The destination IP address
C.The source port number
D.The destination port number ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: When two applications on one host connect to the same remote server, they must use distinct source ports. The destination IP and port remain identical, so the uniqueness comes from the source port, allowing the OS to demultiplex the traffic correctly. Thus option D is correct.

Q18. A firewall rule allows traffic to ports 2000‑3000 and 4000‑5000 but denies 2500‑4500. What is the resulting effective behavior for port 3500?

A.Allowed, because it falls within 2000‑3000.
B.Denied, because the deny rule overrides the allow rule for overlapping ranges. ✅
C.Allowed, because it falls within 4000‑5000.
D.Denied, because 3500 is not explicitly mentioned in any rule.
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The deny rule covers 2500‑4500, which includes 3500, and typically takes precedence over allow rules for overlapping ranges. However, because the allow rule for 4000‑5000 does not overlap with 3500, the deny still applies, making the traffic blocked. Therefore the correct answer is that port 3500 is denied, which corresponds to option B. (Correct answer: B)

Q19. When using IPv6, does the larger address space affect the size or range of port numbers available for applications?

A.IPv6 doubles the port range to 0‑131071.
B.IPv6 reduces the port range to 0‑32767.
C.IPv6 does not change the 16‑bit port space; the range remains 0‑65535. ✅
D.IPv6 removes the concept of ports entirely.
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: IPv6 expands the IP address length but retains the traditional 16‑bit transport‑layer port field. Consequently, the range of ports stays at 0‑65535, identical to IPv4. The benefit of IPv6 lies in address abundance, not in altering port numbers. Therefore option C is correct.

Q20. How do port numbers enable multiplexing of multiple applications over a single IP address?

A.By assigning each application a unique IP address. ✅
B.By using the port field to differentiate traffic for each application.
C.By encrypting each application's data with a distinct key.
D.By requiring each application to use a different network interface.
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Multiplexing relies on the 16‑bit port field in the transport header. Each application binds to a distinct port number, allowing the operating system to direct incoming packets to the correct process while sharing the same IP address. This mechanism makes option A the accurate description of how ports facilitate multiplexing.

Q21. What happens if a non‑privileged user attempts to bind a server socket to port 80 on a Unix‑like system?

A.The binding succeeds without any warnings.
B.The operating system automatically elevates the user’s privileges.
C.The bind call fails with a permission error. ✅
D.The socket is created but cannot accept connections.
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: On Unix‑like systems, ports below 1024 are privileged and require root or equivalent capabilities. When a regular user tries to bind to port 80, the system returns an error (typically EACCES). The socket may be created, but it cannot be bound successfully, making option C the correct outcome.

Q22. Enabling port forwarding on a home router for an internal web server (192.168.1.20:80) to external port 8080 results in which behavior?

A.All inbound traffic to port 80 on the router is redirected to the internal server.
B.Inbound traffic to port 8080 on the router is forwarded to 192.168.1.20:80. ✅
C.The router blocks all incoming connections to the internal server.
D.Port forwarding only works for UDP, not TCP.
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Port forwarding maps a specific external port (here 8080) to an internal host and port (192.168.1.20:80). Consequently, any connection attempts to the router’s public IP on port 8080 are transparently sent to the internal web server. The other statements misrepresent the function, so option B correctly describes the behavior.

🔗 Related Topics (MCQs)