Courseiva

CCNA Network Programming Questions

32 questions · Network Programming · All types, answers revealed

1
MCQmedium

You are configuring a server to handle high-concurrency requests. Why is it preferable to use the select.select() module over a simple blocking recv() loop?

A.It reduces the CPU usage of the Python interpreter during idle times.
B.It automatically implements multi-threaded socket handling.
C.It automatically handles TCP flow control packets.
D.It enables monitoring multiple socket descriptors for I/O readiness.
AnswerD

select permits waiting for multiple sockets without blocking the thread.

Why this answer

select.select() allows monitoring multiple sockets simultaneously, preventing the server from blocking on a single idle connection.

2
MCQhard

In a multi-threaded Python server, why is it discouraged to share a single socket object across multiple threads without synchronization?

A.Python's GIL prevents concurrent socket access.
B.Concurrent access can lead to interleaved or corrupted data streams.
C.Sockets are not picklable.
D.The socket module raises a RuntimeError for concurrent usage.
AnswerB

Shared descriptors without locking can cause race conditions in the application logic.

Why this answer

Socket operations are generally thread-safe in CPython, but overlapping operations on the same file descriptor can lead to interleaved data or corrupted state.

3
Multi-Selectmedium

Which TWO of the following are true regarding the socket.shutdown() method?

Select 2 answers
A.It can be used to disable only the receiving side of the socket.
B.It is identical to the close() method.
C.It causes an immediate memory deallocation of the socket object.
D.It can be used to disable only the sending side of the socket.
E.It automatically reopens the socket if called twice.
AnswersA, D

SHUT_RD stops the input stream.

Why this answer

shutdown() can disable reading, writing, or both, which is useful for protocol-level signaling.

4
MCQeasy

Which of the following is the correct way to close a socket object in Python?

A.socket.destroy()
B.socket.shutdown()
C.socket.close()
D.del socket
AnswerC

close() is the standard way to release the socket resource.

Why this answer

The close() method should be called on the socket object to release the underlying file descriptor.

5
MCQeasy

Which method should a developer use to retrieve the IP address and port of the remote peer connected to a TCP socket?

A.socket.recvfrom()
B.socket.getpeername()
C.socket.getsockname()
D.socket.getfqdn()
AnswerB

getpeername() is the correct method for remote connection details.

Why this answer

getpeername() returns the address of the remote endpoint connected to the socket.

6
Multi-Selecteasy

Which TWO of the following are necessary to connect a TCP client to a server?

Select 2 answers
A.socket.listen()
B.socket.bind()
C.socket.socket()
D.socket.connect()
E.socket.accept()
AnswersC, D

Required to create the socket object.

Why this answer

The client must have an instantiated socket and call connect() with the correct target address tuple.

7
Multi-Selecthard

Which THREE of the following options can be used with socket.setsockopt()?

Select 3 answers
A.socket.TCP_BUFFER_SIZE
B.socket.SO_KEEPALIVE
C.socket.SO_REUSEADDR
D.socket.TCP_NODELAY
E.socket.SO_MAX_CONNECTIONS
AnswersB, C, D

Enables keep-alive packets.

Why this answer

SO_REUSEADDR, SO_KEEPALIVE, and TCP_NODELAY are standard options supported by most systems.

8
MCQeasy

What is the effect of setting a socket to non-blocking mode using socket.setblocking(False)?

A.It makes the socket perform asynchronous DNS lookups.
B.It increases the throughput of the connection.
C.It automatically closes the socket if it is idle.
D.It raises an exception if an operation would block.
AnswerD

Operations that cannot complete immediately throw a BlockingIOError.

Why this answer

Non-blocking sockets cause operations like recv() and connect() to raise an exception if they cannot be completed immediately.

9
Multi-Selecthard

Which THREE of the following are true regarding the use of the socket module in Python?

Select 3 answers
A.Sockets are identified by file descriptors in the OS.
B.Non-blocking sockets raise BlockingIOError if an operation cannot complete.
C.The socket.socket() function is a factory for socket objects.
D.The send() method is guaranteed to send the entire buffer requested.
E.Closing a socket does not terminate the underlying file descriptor.
AnswersA, B, C

Sockets are file-like objects backed by OS descriptors.

Why this answer

Sockets are file descriptors, they can be set to non-blocking mode, and they must be closed to avoid resource leaks.

10
Multi-Selecteasy

Which TWO of the following are differences between TCP and UDP?

Select 2 answers
A.UDP is connection-oriented.
B.UDP is always faster than TCP in every network condition.
C.TCP has higher overhead than UDP.
D.TCP guarantees packet delivery.
E.TCP supports multicasting natively.
AnswersC, D

Handshaking and flow control add overhead to TCP.

Why this answer

TCP is reliable and connection-oriented, whereas UDP is connectionless and unreliable.

11
MCQmedium

Why is the socket.socket() constructor typically called with socket.AF_INET and socket.SOCK_STREAM for a standard web server?

A.To provide a reliable, connection-oriented byte stream.
B.To enable UDP broadcast.
C.To reduce latency for real-time video.
D.To support raw IP packet injection.
AnswerA

SOCK_STREAM is the identifier for the TCP protocol.

Why this answer

AF_INET defines IPv4 and SOCK_STREAM defines the TCP protocol, which are standard for HTTP.

12
MCQmedium

You need to detect if a client has disconnected from your TCP server. What is the standard behavior of recv() when the peer performs a clean shutdown?

A.It raises a ConnectionResetError.
B.It returns None.
C.It blocks indefinitely.
D.It returns an empty string (b'').
AnswerD

An empty bytes object is the indicator of an orderly EOF in TCP.

Why this answer

A clean shutdown by the peer causes recv() to return an empty bytes object (b'').

13
MCQhard

Which of the following describes the behavior of TCP_NODELAY?

A.It disables Nagle's algorithm for low-latency communication.
B.It forces the socket to ignore packet loss.
C.It enables UDP-like performance for TCP.
D.It increases the packet size to match MTU.
AnswerA

This allows small packets to be sent without waiting for buffer filling.

Why this answer

TCP_NODELAY disables Nagle's algorithm, which otherwise buffers small packets to send them as one, reducing network overhead but increasing latency.

14
MCQeasy

Which function in the socket module is used to convert a 32-bit integer from host byte order to network byte order?

A.socket.ntohl()
B.socket.htons()
C.socket.htonl()
D.socket.inet_aton()
AnswerC

htonl is used for 32-bit integer conversion.

Why this answer

htonl() stands for Host to Network Long, converting 32-bit integers for network transmission.

15
Multi-Selecteasy

Which THREE of the following are valid ways to send or receive data?

Select 3 answers
A.socket.send()
B.socket.sendall()
C.socket.recv()
D.socket.transfer()
E.socket.push()
AnswersA, B, C

Standard method to send bytes.

Why this answer

recv(), send(), and sendall() are the primary methods for data transfer in Python sockets.

16
Multi-Selecthard

Which TWO of the following are common issues when using select.select()?

Select 2 answers
A.It requires root privileges to execute.
B.It is significantly faster than epoll on all platforms.
C.It is inefficient for monitoring thousands of concurrent sockets.
D.It has a limited maximum number of sockets it can monitor.
E.It cannot be used with non-blocking sockets.
AnswersC, D

select has an O(n) performance characteristic that degrades with many sockets.

Why this answer

select is limited by the maximum number of file descriptors (FD_SETSIZE) and is not as efficient as epoll/kqueue for massive numbers of connections.

17
MCQeasy

What is the purpose of the socket.recvfrom() method?

A.It retrieves data and the sender's address in a single call.
B.It is for TCP servers to accept new connections.
C.It forces a socket to switch to UDP mode.
D.It is used to receive large files in chunks.
AnswerA

This is essential for UDP, where no permanent connection exists.

Why this answer

recvfrom() is designed for connectionless protocols like UDP to receive data and return the address of the sender.

18
MCQhard

You are using socket.socket(socket.AF_INET, socket.SOCK_DGRAM). You attempt to send 10,000 bytes via sendto(). Why might the operation raise an OSError?

A.The socket must be set to non-blocking mode to send large data.
B.UDP is connection-oriented and requires connect() first.
C.The datagram size exceeds the maximum packet size allowed by the network stack.
D.TCP handshaking is missing for UDP.
AnswerC

OS network buffers have limits; large UDP datagrams may trigger errors if they exceed the MTU or buffer capacity.

Why this answer

UDP packets are limited by the MTU of the network path; trying to send a datagram larger than the buffer/MTU limits can cause an OS-level error.

19
Multi-Selectmedium

Which TWO of the following methods are typically used on a TCP server socket?

Select 2 answers
A.socket.listen()
B.socket.sendall()
C.socket.recvfrom()
D.socket.bind()
E.socket.connect()
AnswersA, D

Required to transition the socket to passive mode.

Why this answer

A TCP server uses bind() to assign the address and listen() to prepare for incoming connections.

20
MCQhard

When handling TCP streams, why might recv(1024) return fewer than 1024 bytes even if the sender sent more?

A.The packet was dropped due to network congestion.
B.TCP is a stream protocol; it does not guarantee message boundaries.
C.The socket was set to non-blocking mode.
D.The OS limited the receive window size.
AnswerB

recv() returns whatever data is currently in the receive buffer, not necessarily the full message.

Why this answer

TCP is a stream-oriented protocol, not a message-oriented one. Data is delivered based on buffer availability and segmentation.

21
MCQhard

When using select.select(inputs, outputs, exceptions), what does the 'outputs' list signify?

A.Sockets that are ready to send data without blocking.
B.Sockets that are closed for writing.
C.Sockets that have finished writing.
D.Sockets that have received an interrupt signal.
AnswerA

Writing to these sockets will not trigger a block.

Why this answer

The 'outputs' list contains sockets that are monitored for readiness to accept data for writing without blocking.

22
MCQmedium

You are building an application using SSL/TLS. Which module must be used to wrap a standard socket to provide encrypted communication?

A.cryptography
B.hashlib
C.ssl
D.socket.ssl
AnswerC

The ssl module provides the wrap_socket or context-based wrapping functionality.

Why this answer

The ssl module provides the necessary wrappers to enable TLS/SSL over standard socket objects.

23
MCQhard

When using socket.setsockopt(), which option is required to immediately reuse a port that is currently in a TIME_WAIT state after a server restart?

A.socket.TCP_NODELAY
B.socket.SO_REUSEADDR
C.socket.SO_LINGER
D.socket.SO_KEEPALIVE
AnswerB

SO_REUSEADDR prevents EADDRINUSE errors on bind after a quick restart.

Why this answer

SO_REUSEADDR allows a socket to bind to an address/port that is currently in the TIME_WAIT state.

24
Multi-Selecteasy

Which THREE of the following are valid address families or socket types used with the Python socket module?

Select 3 answers
A.socket.AF_TCP
B.socket.AF_INET
C.socket.AF_INET6
D.socket.SOCK_LOCAL
E.socket.SOCK_STREAM
AnswersB, C, E

Standard IPv4 family.

Why this answer

AF_INET and AF_INET6 are common address families, while SOCK_STREAM and SOCK_DGRAM are standard socket types.

25
MCQmedium

You are configuring a socket timeout. What happens if you call socket.settimeout(5.0) and the operation exceeds 5 seconds?

A.The socket automatically closes.
B.The operation returns None.
C.The kernel silently drops the connection.
D.A socket.timeout exception is raised.
AnswerD

This is the standard mechanism for handling socket delays.

Why this answer

A timeout sets a limit on blocking operations; if the time is exceeded, a socket.timeout exception is raised.

26
MCQmedium

What is the significance of the SO_BROADCAST option in a UDP socket?

A.It enables the socket to send packets to a broadcast address.
B.It allows the socket to handle incoming multicast traffic.
C.It allows the socket to listen to all ports on a machine.
D.It makes the socket connection-oriented.
AnswerA

By default, systems disallow broadcasting for safety; this option unlocks it.

Why this answer

SO_BROADCAST is a prerequisite for sending packets to the broadcast address on a network interface.

27
MCQeasy

In a Python TCP server, what is the primary purpose of the backlog parameter in the listen(backlog) method?

A.It sets the timeout for idle client sockets.
B.It limits the total number of connected clients.
C.It specifies the maximum number of bytes per packet.
D.It defines the maximum length of the queue of pending connections.
AnswerD

The backlog value represents the number of unaccepted connections the system will queue.

Why this answer

The backlog defines the maximum number of queued connections allowed before the OS starts rejecting new ones.

28
MCQhard

When implementing a custom application protocol over TCP, how should you handle message framing?

A.Use a unique delimiter or a length prefix header for each message.
B.Rely on recv() returning one complete message at a time.
C.Call sleep() between sends to ensure packet separation.
D.Use UDP instead for automatic framing.
AnswerA

Framing techniques are necessary to parse streams into logical units.

Why this answer

Since TCP is a stream, you must implement a protocol layer (like length-prefixing) to know when one message ends and the next begins.

29
MCQmedium

You are writing a UDP server. What is the main disadvantage of using UDP for high-volume data transmission compared to TCP?

A.UDP packets are always larger than TCP packets.
B.UDP does not provide delivery guarantees or ordering.
C.UDP requires more handshake overhead.
D.UDP is not supported on all operating systems.
AnswerB

UDP is unreliable, whereas TCP provides reliability features.

Why this answer

UDP provides no guarantee of delivery, order, or congestion control, requiring the application to handle these issues.

30
MCQmedium

You are designing a cross-platform network application. Why is it recommended to use socket.getaddrinfo() instead of socket.gethostbyname()?

A.It caches results to reduce DNS requests.
B.It supports IPv6 and is more portable across different network stacks.
C.It is faster than gethostbyname().
D.It automatically resolves local hostnames.
AnswerB

getaddrinfo is the modern, recommended standard for address resolution.

Why this answer

getaddrinfo() is protocol-agnostic (IPv4/IPv6) and returns structured information suitable for socket creation.

31
Multi-Selectmedium

Which THREE of the following socket attributes or methods are relevant to managing socket timeouts?

Select 3 answers
A.socket.settimeout()
B.socket.gettimeout()
C.socket.listen()
D.socket.timeout
E.socket.setblocking()
AnswersA, B, D

Sets the timeout duration.

Why this answer

settimeout() sets the delay, gettimeout() retrieves it, and the socket might raise a timeout exception.

32
MCQeasy

Which socket method is used to bind a socket to a specific address and port?

A.socket.accept()
B.socket.connect()
C.socket.bind()
D.socket.listen()
AnswerC

bind() is the correct method for assigning address/port.

Why this answer

The bind() method associates a socket with a specific address and port tuple.

Ready to test yourself?

Try a timed practice session using only Network Programming questions.