Home »
MCQs
Python Network Programming MCQs (Multiple-Choice Questions)
Python provides several built-in modules for network programming, including socket, urllib, http.server, ssl, select, and selectors. These modules can be used to create network clients and servers, communicate using TCP and UDP, work with URLs, and handle network connections.
Python Network Programming MCQs
These Python Network Programming MCQs cover important concepts such as sockets, TCP, UDP, IP addresses, ports, DNS, client-server communication, HTTP requests, URL handling, and network-related Python modules.
List of Python Network Programming MCQs
The following Python Network Programming MCQs are designed to test your understanding of Python networking concepts and their implementation.
1. Which Python module provides a low-level networking interface?
- network
- socket
- internet
- web
Answer: B) socket
Explanation:
The socket module provides access to the BSD socket interface and is used for low-level network communication in Python.
2. Which socket type is commonly used for TCP communication?
- SOCK_RAW
- SOCK_DGRAM
- SOCK_STREAM
- SOCK_PACKET
Answer: C) SOCK_STREAM
Explanation:
SOCK_STREAM is used for stream-oriented communication such as TCP.
3. Which socket type is commonly associated with UDP communication?
- SOCK_STREAM
- SOCK_DGRAM
- SOCK_FILE
- SOCK_HTTP
Answer: B) SOCK_DGRAM
Explanation:
SOCK_DGRAM is used for datagram-based communication such as UDP.
4. Which constant represents the IPv4 address family in Python?
- socket.AF_IPV4
- socket.IPV4
- socket.AF_INET
- socket.INET4
Answer: C) socket.AF_INET
Explanation:
socket.AF_INET represents the IPv4 address family.
5. Which constant represents the IPv6 address family?
- socket.AF_INET6
- socket.IPV6
- socket.AF_IP6
- socket.INET6_ONLY
Answer: A) socket.AF_INET6
Explanation:
socket.AF_INET6 represents the IPv6 address family.
6. Which function is used to create a socket object?
- socket.create()
- socket.socket()
- socket.open()
- socket.connect()
Answer: B) socket.socket()
Explanation:
The socket.socket() constructor creates a new socket object.
7. Which method connects a client socket to a remote address?
- connect()
- bind()
- listen()
- accept()
Answer: A) connect()
Explanation:
The connect() method connects a socket to a remote socket address.
8. Which socket method associates a socket with a local address?
- connect()
- bind()
- attach()
- listen()
Answer: B) bind()
Explanation:
The bind() method associates a socket with a specified local address.
9. Which method puts a TCP server socket into listening mode?
- wait()
- listen()
- accept()
- serve()
Answer: B) listen()
Explanation:
The listen() method enables a server socket to accept incoming connections.
10. Which method accepts an incoming connection on a listening socket?
- receive()
- connect()
- accept()
- listen()
Answer: C) accept()
Explanation:
The accept() method accepts an incoming connection and returns a new socket along with the client's address.
11. What does the socket recv() method return?
- A string only
- A bytes object
- An integer only
- A socket object
Answer: B) A bytes object
Explanation:
The recv() method receives data from a socket and returns it as a bytes object.
12. Which method can be used to send bytes through a connected socket?
- send()
- write()
- push()
- transmit()
Answer: A) send()
Explanation:
The send() method sends data through a connected socket.
13. Which socket method attempts to send all data provided to it?
- sendall()
- sendfull()
- sendbytes()
- writeall()
Answer: A) sendall()
Explanation:
sendall() continues sending data until all data has been sent or an error occurs.
14. Which sequence is normally used to create a basic TCP server?
- socket(), connect(), send(), close()
- socket(), bind(), listen(), accept()
- socket(), send(), bind(), listen()
- bind(), socket(), connect(), accept()
Answer: B) socket(), bind(), listen(), accept()
Explanation:
A typical TCP server creates a socket, binds it to an address, starts listening, and then accepts incoming connections.
15. Which operation is normally required by a basic TCP client to connect to a server?
- listen()
- accept()
- connect()
- bind()
Answer: C) connect()
Explanation:
A TCP client normally creates a socket and uses connect() to establish a connection with the server.
16. What does the close() method do for a socket?
- Starts a server
- Closes the socket and releases its associated resource
- Changes the IP address
- Changes the port number
Answer: B) Closes the socket and releases its associated resource
Explanation:
The close() method marks the socket as closed and releases its associated system resource.
17. Which method can be used to set a timeout on a socket?
- settimeout()
- setwait()
- timeout()
- setdelay()
Answer: A) settimeout()
Explanation:
The settimeout() method sets the timeout value for socket operations.
18. Which method can change a socket between blocking and non-blocking mode?
- setblocking()
- setmode()
- switchblocking()
- block()
Answer: A) setblocking()
Explanation:
The setblocking() method controls whether socket operations are blocking or non-blocking.
19. Which method returns the local address of a socket?
- getpeername()
- getsockname()
- getlocal()
- getaddress()
Answer: B) getsockname()
Explanation:
getsockname() returns the socket's own address.
20. Which method returns the remote address connected to a socket?
- getsockname()
- getremote()
- getpeername()
- getclient()
Answer: C) getpeername()
Explanation:
getpeername() returns the address of the remote endpoint connected to the socket.
21. Which Python module provides functions for working with URLs?
- urltools
- urllib
- weburl
- httpurl
Answer: B) urllib
Explanation:
The urllib package contains modules for opening, reading, and parsing URLs.
22. Which module is used to open and read URLs?
- urllib.request
- urllib.open
- urllib.http
- urllib.web
Answer: A) urllib.request
Explanation:
The urllib.request module provides functions and classes for opening URLs and making HTTP-related requests.
23. Which function is commonly used with urllib.request to open a URL?
- openurl()
- urlopen()
- requesturl()
- fetchurl()
Answer: B) urlopen()
Explanation:
urllib.request.urlopen() is used to open a URL and obtain a response object.
24. Which module is used to parse URLs into their components?
- urllib.parse
- urllib.split
- urllib.url
- urllib.components
Answer: A) urllib.parse
Explanation:
The urllib.parse module provides functions for parsing and constructing URLs.
25. Which function can parse a URL into its components?
- parseurl()
- urlparse()
- spliturl()
- geturlparts()
Answer: B) urlparse()
Explanation:
urllib.parse.urlparse() parses a URL into components such as scheme, network location, path, parameters, query, and fragment.
26. Which Python module contains exceptions raised by urllib.request?
- urllib.exceptions
- urllib.error
- urllib.request_error
- urllib.fail
Answer: B) urllib.error
Explanation:
The urllib.error module defines exceptions raised by urllib.request.
27. Which module can parse robots.txt files?
- urllib.robotparser
- urllib.robots
- urllib.parserrobots
- urllib.txtparser
Answer: A) urllib.robotparser
Explanation:
The urllib.robotparser module provides functionality for parsing robots.txt files.
28. Which module provides classes for implementing HTTP servers in Python?
- http.server
- http.host
- http.service
- web.server
Answer: A) http.server
Explanation:
The http.server module provides classes for implementing HTTP servers.
29. Which class creates a basic HTTP server in Python?
- HTTPServer
- WebServer
- HTTPService
- HTTPHost
Answer: A) HTTPServer
Explanation:
http.server.HTTPServer builds on socketserver.TCPServer and provides a basic HTTP server.
30. Which class is commonly used to handle HTTP requests in a basic Python HTTP server?
- BaseHTTPRequestHandler
- HTTPRequest
- RequestHandler
- HTTPHandlerBase
Answer: A) BaseHTTPRequestHandler
Explanation:
BaseHTTPRequestHandler provides the base class for handling HTTP requests in http.server.
31. Which method can be overridden in a BaseHTTPRequestHandler subclass to handle HTTP GET requests?
- get()
- do_GET()
- handle_GET()
- request_GET()
Answer: B) do_GET()
Explanation:
A request handler can implement do_GET() to handle HTTP GET requests.
32. Which method can be overridden to handle HTTP POST requests?
- do_POST()
- post()
- handle_POST()
- request_POST()
Answer: A) do_POST()
Explanation:
The do_POST() method is used by a request handler to process HTTP POST requests.
33. Which method starts serving requests continuously for an HTTPServer?
- start()
- serve()
- serve_forever()
- run_server()
Answer: C) serve_forever()
Explanation:
serve_forever() processes requests until the server is shut down.
34. Which Python module provides asynchronous I/O capabilities?
- asyncio
- asyncnet
- networkasync
- aioonly
Answer: A) asyncio
Explanation:
The asyncio module provides infrastructure for writing concurrent code using asynchronous I/O.
35. Which Python module provides high-level I/O multiplexing?
- selectors
- multiplex
- ioselect
- socketselect
Answer: A) selectors
Explanation:
The selectors module provides high-level I/O multiplexing based on operating-system mechanisms.
36. Which Python module provides low-level waiting for I/O completion?
- select
- waitio
- iowait
- polling
Answer: A) select
Explanation:
The select module provides access to platform-specific I/O monitoring mechanisms.
37. Which module provides a TLS/SSL wrapper for socket objects?
- security
- ssl
- tls
- socketsecurity
Answer: B) ssl
Explanation:
The ssl module provides TLS/SSL support for network sockets.
38. Which method is commonly used to shut down part or all of a socket connection?
- terminate()
- shutdown()
- stop()
- disconnect()
Answer: B) shutdown()
Explanation:
The shutdown() method can be used to disable sending, receiving, or both directions of a socket connection.
39. Which function can resolve a hostname to an IP address?
- socket.gethostbyname()
- socket.resolve()
- socket.hostname_to_ip()
- socket.findhost()
Answer: A) socket.gethostbyname()
Explanation:
socket.gethostbyname() resolves a hostname to an IPv4 address.
40. Which function provides information about a host name and address?
- socket.gethostbyname_ex()
- socket.hostinfo()
- socket.getdns()
- socket.hostdetails()
Answer: A) socket.gethostbyname_ex()
Explanation:
gethostbyname_ex() returns extended information about a host name, including aliases and IPv4 addresses.
41. Which function provides address information suitable for creating a socket?
- socket.getaddrinfo()
- socket.addressinfo()
- socket.resolveinfo()
- socket.getipinfo()
Answer: A) socket.getaddrinfo()
Explanation:
getaddrinfo() translates a host and service into a sequence of address information suitable for creating and connecting sockets.
42. What is the purpose of a port number in network communication?
- It identifies a specific network interface only
- It identifies a service or endpoint on a host
- It replaces the IP address
- It encrypts network data
Answer: B) It identifies a service or endpoint on a host
Explanation:
A port number helps identify the network service or endpoint with which an application communicates on a host.
43. What is the main characteristic of TCP communication?
- It is connection-oriented
- It always broadcasts packets
- It does not use ports
- It is only used for DNS
Answer: A) It is connection-oriented
Explanation:
TCP uses a connection-oriented stream communication model. Python commonly represents TCP sockets with SOCK_STREAM.
44. Which characteristic is associated with UDP?
- Connection-oriented stream communication
- Datagram-based communication
- Mandatory connection establishment
- Guaranteed delivery by the socket API
Answer: B) Datagram-based communication
Explanation:
UDP is a datagram-oriented protocol and is commonly used with SOCK_DGRAM sockets.
45. Which method is commonly used with a UDP socket to send a datagram to a specified address?
- sendto()
- sendaddress()
- sendpacket()
- sendudp()
Answer: A) sendto()
Explanation:
The sendto() method sends data to a specified address and is commonly used with datagram sockets.
46. Which method can receive a UDP datagram along with the sender's address?
- recvfrom()
- receivefrom()
- getpacket()
- recvaddress()
Answer: A) recvfrom()
Explanation:
The recvfrom() method receives data from a datagram socket and returns the data together with the sender's address.
47. Which exception is associated with address-related errors from functions such as getaddrinfo()?
- socket.gaierror
- socket.addressError
- socket.IPError
- socket.HostError
Answer: A) socket.gaierror
Explanation:
socket.gaierror is an exception raised for address-related errors from functions such as getaddrinfo() and getnameinfo().
48. What does socket.create_connection() provide?
- A higher-level way to connect to a TCP service
- A way to create only UDP sockets
- An HTTP server
- A DNS server
Answer: A) A higher-level way to connect to a TCP service
Explanation:
socket.create_connection() provides a higher-level way to connect to a TCP service using a host and port.
49. Which statement about Python's built-in http.server module is correct?
- It is recommended as a production-grade web server
- It provides only basic HTTP server functionality and is not recommended for production
- It only supports FTP
- It can only create UDP servers
Answer: B) It provides only basic HTTP server functionality and is not recommended for production
Explanation:
Python's documentation states that http.server implements basic HTTP server functionality and is not recommended for production use because it provides only basic security checks.
50. Which statement best describes Python network programming?
- It is limited to creating web pages
- It allows applications to communicate over networks using modules such as socket, urllib, asyncio, and http.server
- It only supports TCP
- It requires a third-party package for every type of network communication
Answer: B) It allows applications to communicate over networks using modules such as socket, urllib, asyncio, and http.server
Explanation:
Python's standard library includes several networking and interprocess communication modules, including socket, urllib, asyncio, ssl, select, selectors, and http.server.
Advertisement
Advertisement