UNIX Socket Programming MCQs: 10 Solved Socket API Questions with Explanations

Solve nine UNIX socket programming MCQs and one descriptive connected-UDP check. Each answer explains the API role, descriptor flow, or calculation behind it.

KnowledgeGate Team

Exam prep & CS education

Updated 19 Nov 20268 min read
UNIX Socket Programming MCQs: 10 Solved Socket API Questions with Explanations

Wrong answers begin when socket creation, local-address assignment, passive listening, and client acceptance blur together. Track the listening descriptor separately from each accepted descriptor, then treat connected UDP as single-peer selection. Use the earlier TCP and UDP MCQs for protocol properties and transport-layer behaviour; use these UNIX socket questions for call order, descriptor flow, and peer reassignment.

1. UNIX socket API map before the questions

UNIX socket API map showing TCP server, TCP client, UDP call order, and descriptor flow.

A socket is one endpoint used by a process for network communication. Keep these three paths separate:

  • TCP server: socket() -> bind() -> listen() -> accept() -> recv()

  • TCP client: socket() -> connect() -> send()/recv()

  • Unconnected UDP: socket() -> sendto()/recvfrom()

Suppose socket() returns listening descriptor fd = 3. bind() associates it with 192.0.2.10:8080, and listen(3, 5) makes it passive with requested backlog 5. For client 198.51.100.24:53000, accept() returns connected descriptor fd = 7. Read client bytes with recv(7, ...), not recv(3, ...).

Function

Role

socket()

Creates an endpoint

bind()

Assigns a local address

listen()

Marks a bound TCP socket passive

accept()

Returns a new connected socket

connect()

Selects a remote peer

send(..., 0)

Has write()-like semantics on a connected socket

poll()

Checks readiness or state events

2. Questions 1-2: endpoints and non-blocking send

Question 1: identify the network endpoint

Asked in: BPSC 2024

Question: What is the term for an endpoint of an inter-process communication flow across a computer network?

  • (a) Port

  • (b) Pipe

  • (c) Socket

  • (d) More than one of the above

  • (e) None of the above

Answer: (c) Socket. A port is one address component. A socket is the process's networking endpoint. A TCP socket can involve 192.0.2.10:8080 and protocol TCP, though that pair alone is not a globally unique connection.

Question 2: what a non-blocking send allows

Asked in: Hexaware 2023

Question: In the non blocking send __________

  • (a) the sending process keeps sending until the message is received

  • (b) the sending process sends the message and resumes operation

  • (c) the sending process keeps sending until it receives a message

  • (d) none of the mentioned

Answer: (b) the sending process sends the message and resumes operation. Non-blocking governs whether the caller waits, not delivery. After attempting 128 bytes, execution continues. The API can return fewer bytes or a would-block error, so check its return value.

3. Questions 3-5: bind, listen, accept, and recv

Question 3: convert an active TCP socket into a passive socket

Asked in: GATE 2014, Set 2

Question: Which of the following socket API functions converts an unconnected active TCP socket into a passive socket?

  • (a) connect

  • (b) bind

  • (c) listen

  • (d) accept

Answer: (c) listen. bind() gives the local address, while listen() makes the socket passive. accept() then returns a different connected socket for one client.

Question 4: order the server calls

Asked in: GATE 2015, Set 2

Question: Identify the correct order in which a server process must invoke the function calls accept, bind, listen, and recv according to UNIX socket API.

  • (a) listen, accept, bind, recv

  • (b) bind, listen, accept, recv

  • (c) bind, accept, listen, recv

  • (d) accept, listen, bind, recv

Answer: (b) bind, listen, accept, recv. Bind fd = 3 to 192.0.2.10:8080, call listen(3, 5), receive connected fd = 7 from accept(3, ...), then call recv(7, buffer, 256, 0). recv(3, ...) targets the wrong descriptor because fd = 3 remains the listener.

Question 5: match each server-side function to its role

Asked in: UGC NET 2022, June

Question: Match List I with List II:

List I

List II

(A) BIND

(I) Block the caller until a connection attempt arrives

(B) LISTEN

(II) Give a local address to a socket

(C) ACCEPT

(III) Show willingness to accept connections

(D) SOCKET

(IV) Create a new point

Choose the correct answer from the options given below:

  • (a) (A)-(I), (B)-(III), (C)-(II), (D)-(IV)

  • (b) (A)-(II), (B)-(III), (C)-(I), (D)-(IV)

  • (c) (A)-(III), (B)-(II), (C)-(I), (D)-(IV)

  • (d) (A)-(I), (B)-(II), (C)-(III), (D)-(IV)

Answer: (b) (A)-(II), (B)-(III), (C)-(I), (D)-(IV). bind() gives the address, listen() shows willingness, accept() returns the per-client socket, and socket() creates the endpoint. Blocking accept() waits on an empty queue; non-blocking accept() can return a would-block error.

4. Questions 6-7: connected UDP selects one peer at a time

Connected UDP timeline showing peer replacement on fd 5 and receive filtering after reconnect.

Question 6: decide which connected-UDP statement is correct

Asked in: GATE 2017, Set 2

Question: Consider socket API on a Linux machine that supports connected UDP sockets. A connected UDP socket is a UDP socket on which connect function has already been called. Which of the following statements is/are CORRECT? I. A connected UDP socket can be used to communicate with multiple peers simultaneously. II. A process can successfully call connect function again for an already connected UDP socket.

  • (a) I only

  • (b) II only

  • (c) Both I and II

  • (d) Neither I nor II

Answer: (b) II only. UDP connect() records one default peer and filters ordinary receives to it, so I is false. On Linux, another connect() can replace the peer, so II is true. fd = 5 may select 203.0.113.8:9000, then 203.0.113.9:9001. This is sequential reassignment, not simultaneous communication.

Question 7: trace a connected-UDP peer change

A connected UDP descriptor stores one default peer. A later connect() on Linux replaces that peer rather than adding another simultaneous destination.

Question: Descriptor fd = 5 connects to 203.0.113.8:9000 and sends alpha. The process then connects the same descriptor to 203.0.113.9:9001 and sends beta. Which peer receives each datagram, and can a later datagram from the first peer satisfy an ordinary recv() after the second connection?

Answer: alpha goes to 203.0.113.8:9000, while beta goes to 203.0.113.9:9001. After the second connect(), ordinary receives are filtered to the second peer, so a datagram from the first peer does not satisfy that recv(). Multiple simultaneous peers need separate connected sockets or an unconnected socket with sendto()/recvfrom().

5. Questions 8-9: mixed socket-function traps

Question 8: reject four incorrect protocol statements

Question: Consider the following statements: S1: Bind( ) socket function converts an unconnected active TCP socket into a passive socket. S2: The size of the advertised window can change during the course of UDP. S3: UDP and TCP both are byte-oriented protocols. S4: UDP connection is full-duplex. Which of the above statement(s) are correct?

  • (a) Only S1 and S4

  • (b) Only S1, S2 and S4

  • (c) All S1, S2, S3, S4

  • (d) None

Answer: (d) None. S1 is false because listen(), not bind(), makes TCP passive. S2 is false because UDP has no TCP-style advertised window. S3 is false because UDP preserves datagram boundaries while TCP is a byte stream. S4 is false as written because UDP has no established connection to classify as full-duplex.

Question 9: map five descriptions to five functions

Question: Consider the following socket API functionality: S1: function used on server side to assigns a local socket address. S2: function is used by a TCP client to establish a connection with a TCP server. S3: function converts an unconnected socket into a passive socket. S4: function is similar to write() with zero flag parameter. S5: used to check on the state of a socket.

Option

S1

S2

S3

S4

S5

(a)

Bind()

Connect()

Listen()

recv()

socket()

(b)

Bind()

Connect()

Listen()

send()

poll()

(c)

Connect()

Bind()

Listen()

send()

poll()

(d)

Connect()

Bind()

Listen()

recv()

socket()

Answer: (b) Bind(), Connect(), Listen(), send(), poll(). Check each column: local address means bind; client establishment means connect; passive TCP means listen; zero-flags write() semantics mean send; readiness means poll. Here bind() associates fd = 3 with 192.0.2.10:8080, then listen(3, 5) makes it passive.

6. Question 10: timestamp arithmetic around socket I/O

Question 10: calculate clock offset from a four-timestamp exchange

Asked in: ISRO 2014

Question: Assume the following information:

  • Original timestamp value: 46

  • Receive timestamp value: 59

  • Transmit timestamp value: 60

  • Timestamp at arrival of packet: 69

Which of the following statements is correct?

  • (a) Receive clock should go back by 3 milliseconds

  • (b) Transmit and Receive clocks are synchronized

  • (c) Transmit clock should go back by 3 milliseconds

  • (d) Receive clock should go ahead by 1 milliseconds

Correct analysis: none of the listed options under the standard four-timestamp calculation. A socket program records T1 = 46 before transmission, T2 = 59 on receipt, T3 = 60 on reply, and T4 = 69 on final receipt.

theta = ((T2 - T1) + (T3 - T4)) / 2

= ((59 - 46) + (60 - 69)) / 2

= (13 - 9) / 2 = 2 ms

The round-trip network delay is:

delta = (T4 - T1) - (T3 - T2)

= (69 - 46) - (60 - 59)

= 23 - 1 = 22 ms

The receive-side clock is ahead by 2 ms and should go back by 2 ms. Option (a) gets 3 ms by omitting the 1 ms reply-processing interval from the offset calculation. The four-timestamp result is therefore 2 ms backward, which no listed option states.

7. UNIX socket programming traps, revision key, and next route

Use this quick revision key:

  • Endpoint: socket

  • Local address: bind

  • Passive TCP listener: listen

  • Per-client connected descriptor: accept

  • TCP client peer selection: connect

  • Client data: the descriptor returned by accept

  • Connected UDP: one selected peer at a time, with a later Linux connect() able to replace it

Four traps matter: bind versus listen, listener fd = 3 versus accepted fd = 7, UDP datagrams versus a TCP byte stream, and blocking mode versus delivery guarantees. Revisit TCP vs UDP: Transport Layer Explained for the protocol distinction.

Redo Questions 3, 4, 6, 9, and 10 without notes. Your checks are listen; bind -> listen -> accept -> recv; II only; bind/connect/listen/send/poll; and the corrected offset 2 ms with round-trip delay 22 ms.

Learn the call sequence, track the new descriptor from accept(), and keep TCP flow control away from UDP. Continue with the UNIX Socket Programming PYQ Questions hub for more socket-operation practice. GATE Guidance by Sanchit Sir supplies structured concepts and PYQs, while the GATE CS exam path connects the wider preparation route.