Q.1 What is a socket in computer networking?
A physical network device
An endpoint for sending or receiving data across a network
A type of encryption protocol
A method to store IP addresses
Explanation - A socket is a software abstraction that represents an endpoint in a network communication, used for sending and receiving data.
Correct answer is: An endpoint for sending or receiving data across a network
Q.2 Which function is used in Python to create a socket?
socket.create()
socket.socket()
socket.init()
socket.connect()
Explanation - In Python, the socket.socket() function is used to create a new socket object.
Correct answer is: socket.socket()
Q.3 Which of the following socket types is used for TCP?
SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
SOCK_SEQPACKET
Explanation - TCP is a connection-oriented protocol, and in socket programming, SOCK_STREAM is used for TCP connections.
Correct answer is: SOCK_STREAM
Q.4 Which socket type is used for UDP communication?
SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
SOCK_SEQPACKET
Explanation - UDP is a connectionless protocol and uses SOCK_DGRAM in socket programming.
Correct answer is: SOCK_DGRAM
Q.5 What does the bind() function do in socket programming?
Associates a socket with a specific IP address and port
Closes the socket
Sends data through the socket
Establishes a TCP connection
Explanation - The bind() function assigns a socket to a particular IP address and port number on the host machine.
Correct answer is: Associates a socket with a specific IP address and port
Q.6 Which function is used to listen for incoming connections on a TCP socket?
connect()
accept()
listen()
recv()
Explanation - The listen() function enables a TCP socket to accept incoming connection requests from clients.
Correct answer is: listen()
Q.7 After listen(), which function accepts a connection from a client?
connect()
accept()
recv()
bind()
Explanation - accept() waits for an incoming connection request and returns a new socket for communication with the client.
Correct answer is: accept()
Q.8 Which method is used to connect a client socket to a server in Python?
bind()
connect()
listen()
send()
Explanation - The connect() method is used by a client socket to establish a connection to a server's IP and port.
Correct answer is: connect()
Q.9 What does the send() function do in socket programming?
Receives data from a socket
Sends data to the connected socket
Closes the socket
Connects to a server
Explanation - send() transmits data from the socket to the peer socket that is connected.
Correct answer is: Sends data to the connected socket
Q.10 Which function is used to receive data from a socket?
recv()
send()
listen()
accept()
Explanation - recv() reads incoming data from the socket buffer.
Correct answer is: recv()
Q.11 What is the default blocking behavior of sockets?
Non-blocking
Blocking
Half-blocking
Depends on the OS
Explanation - By default, sockets are in blocking mode, meaning socket operations wait until they complete.
Correct answer is: Blocking
Q.12 Which method is used to close a socket in Python?
shutdown()
close()
disconnect()
terminate()
Explanation - The close() method releases the socket resource and terminates the connection.
Correct answer is: close()
Q.13 What is the role of the AF_INET parameter in socket creation?
Specifies the address family IPv4
Specifies IPv6
Specifies TCP protocol
Specifies UDP protocol
Explanation - AF_INET tells the socket to use the IPv4 address family.
Correct answer is: Specifies the address family IPv4
Q.14 Which address family is used for IPv6 in socket programming?
AF_INET
AF_INET6
AF_UNIX
AF_PACKET
Explanation - AF_INET6 specifies the IPv6 address family for sockets.
Correct answer is: AF_INET6
Q.15 In UDP communication, which function is used to send data?
send()
sendto()
connect()
accept()
Explanation - UDP being connectionless uses sendto() to send data to a specific address and port.
Correct answer is: sendto()
Q.16 Which function is used to receive data from a UDP socket?
recv()
recvfrom()
accept()
connect()
Explanation - recvfrom() receives data from a UDP socket and also returns the sender's address.
Correct answer is: recvfrom()
Q.17 What is the main difference between TCP and UDP sockets?
TCP is connectionless, UDP is connection-oriented
TCP is connection-oriented, UDP is connectionless
TCP is faster than UDP
UDP guarantees delivery, TCP does not
Explanation - TCP ensures reliable connection and data delivery; UDP is faster but connectionless and unreliable.
Correct answer is: TCP is connection-oriented, UDP is connectionless
Q.18 Which socket function can be used to set socket options?
setsockopt()
setsockoptval()
setoption()
configure()
Explanation - setsockopt() allows setting options like timeouts or buffer sizes for a socket.
Correct answer is: setsockopt()
Q.19 What is the use of the backlog parameter in listen()?
Sets the maximum number of queued connections
Specifies timeout for connections
Limits data size sent
Defines socket type
Explanation - Backlog defines how many incoming connections can wait before being accepted.
Correct answer is: Sets the maximum number of queued connections
Q.20 Which module in Python is primarily used for socket programming?
network
socket
sys
os
Explanation - The socket module provides all necessary classes and functions for socket programming in Python.
Correct answer is: socket
Q.21 What does non-blocking mode in sockets allow?
Operations wait until complete
Operations return immediately if they cannot complete
Socket closes automatically
Connection is refused
Explanation - Non-blocking sockets allow operations to return immediately instead of waiting, enabling asynchronous programming.
Correct answer is: Operations return immediately if they cannot complete
Q.22 Which function can shut down one or both halves of a TCP connection?
close()
shutdown()
terminate()
stop()
Explanation - shutdown() disables sending, receiving, or both on a TCP socket without fully closing it.
Correct answer is: shutdown()
Q.23 Which method allows a server to handle multiple clients in Python?
Multithreading or multiprocessing
Using multiple bind() calls
Only one socket per server
Using UDP only
Explanation - Servers use threads or processes to handle multiple client connections simultaneously.
Correct answer is: Multithreading or multiprocessing
Q.24 Which exception is raised in Python if a socket operation times out?
socket.timeout
socket.error
socket.blocked
socket.failed
Explanation - When a socket operation exceeds the specified timeout, the socket.timeout exception is raised.
Correct answer is: socket.timeout
Q.25 Which method can be used to get the local address and port of a socket?
getpeername()
getsockname()
getlocaladdr()
getaddress()
Explanation - getsockname() returns the address and port number that the socket is bound to locally.
Correct answer is: getsockname()
