đ 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.
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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?
đ 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.