Java Socket Programming: TCP, UDP & NIO Interview Questions and Answers
Quiz 1: Socket Programming Fundamentals
Q1: What is the purpose of the ServerSocket class?
Answer: To listen for incoming socket connections
Q2: Which TCP/IP layer is primarily responsible for routing packets between different networks?
Answer: Network layer
Q3: Which method would you use to obtain the local machine’s IP and hostname using InetAddress?
Answer: InetAddress.getLocalHost()
Q4: What is the role of threads in socket programming for concurrent servers?
Answer: Handle multiple client requests concurrently
Q5: What is the key difference between UDP and TCP?
Answer: UDP is connectionless, while TCP establishes a connection
Q6: Which method is used to accept a connection from a client in ServerSocket?
Answer: accept()
Q7: What is a socket in Java?
Answer: A network connection
Q8: In the TCP/IP model, which layer typically carries protocols like HTTP and FTP?
Answer: Application layer
Q9 (Fill in the Blank):
- Blank 1: ServerSocket
- Blank 2: ServerSocket
- Blank 3: Socket
- Blank 4: accept()
Q10: What is the purpose of the DatagramSocket class in Java?
Answer: Sending and receiving datagram packets
Q11 (Matching):
- DatagramSocket → Sends/receives UDP datagram packets
- URL → Accesses resources on the web (e.g., via HTTP)
- Socket → Client-side TCP connection endpoint for communication
- InetAddress → Represents an IP address / resolves hostnames
Q12: How does a concurrent server differ from an iterative server?
Answer: A concurrent server handles multiple clients concurrently, while an iterative server handles one client at a time
Q13: What is the purpose of the accept() method in ServerSocket?
Answer: Listens for incoming client connection requests
Q14: What is the difference between unicast and multicast?
Answer: Unicast is one-to-one communication, while multicast is one-to-many communication
Q15: In a TCP client using java.net.Socket, which operation actually initiates the connection?
Answer: Instantiating the Socket with host and port
Q16: What type of streams does a Socket class provide?
Answer: InputStream and OutputStream
Q17: Which class is used with DatagramSocket to represent a UDP message?
Answer: DatagramPacket
Q18 (Ordering): Order the steps to accept a TCP client connection and send a greeting using ServerSocket.
- Create a ServerSocket bound to a specific port.
- Call accept() on the ServerSocket to obtain a Socket.
- Get the OutputStream from the Socket and wrap it in a writer.
- Write a greeting message to the client and flush/close the writer.
Q19: For video streaming where occasional packet loss is acceptable, which protocol is preferred?
Answer: UDP
Q20: A Java ServerSocket is typically used to:
Answer: Listen for incoming client connections on a port
Quiz 2: Java NIO and Selectors
Q1: In Java NIO, what is the purpose of the ‘SelectionKey.attach(Object)’ method?
Answer: To associate application-specific data (like a buffer or state machine) with a specific channel registered to a Selector.
Q2 (Fill in the Blank):
- Blank 1: Selector.open()
- Blank 2: ServerSocketChannel.open()
- Blank 3: false
- Blank 4: register(selector, SelectionKey.OP_ACCEPT)
- Blank 5: select()
Q3: When using a Selector in Java NIO, what is the significance of the selection key’s ‘ready set’ versus its ‘interest set’?
Answer: The interest set defines what operations the Selector should check for, while the ready set identifies operations the channel is actually prepared to perform.
Quiz 3: Transport Protocols and Multicast
Q1: What is the key difference between UDP and TCP transport protocols in socket programming?
Answer: UDP is connectionless, while TCP establishes a connection
Q2: In the TCP/IP model, which layer typically carries protocols like HTTP and FTP?
Answer: Application layer
Q3: In a typical Java TCP server-client communication, which method does the client use to initiate the connection?
Answer: Socket constructor with server IP and port
Q4: When calling the DatagramChannel.join(InetAddress group, NetworkInterface interf) method, what is the primary purpose of the returned MembershipKey object?
Answer: It serves as a handle to manage or drop the membership and to block specific source addresses.
Q5: When using a Selector in Java NIO, what is the significance of the selection key’s ‘ready set’ versus its ‘interest set’?
Answer: The interest set defines what operations the Selector should check for, while the ready set identifies operations the channel is actually prepared to perform.
Q6: When calling the DatagramChannel.join(InetAddress group, NetworkInterface interf) method, what is the primary purpose of the returned MembershipKey object?
Answer: It serves as a handle to manage or drop the membership and to block specific source addresses.
Code Example: Opening and Binding a DatagramChannel
try (DatagramChannel dchannel = DatagramChannel.open(StandardProtocolFamily.INET)) {
...
dchannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT));
...
dchannel.send(echoBuffer, clientSocketAddress);
}
try (DatagramChannel dchannel = DatagramChannel.open(StandardProtocolFamily.INET)){
...
dchannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT));
...
dchannel.send(echoBuffer, clientSocketAddress);
}