Java Networking Quiz - MCQ - Multiple Choice Questions

Welcome to the Java Networking Quiz! If you're new to Java or just want to brush up on your networking basics, you're in the right place. Let's dive into the questions to test your foundational knowledge of Java networking!

1. Which Java package is primarily used for networking?

a) java.net
b) java.util
c) java.io
d) java.swing

Answer:

a) java.net

Explanation:

The java.net package provides the classes for implementing networking applications.

2. Which class is used to create a server socket?

a) Socket
b) ServerSocket
c) NetSocket
d) HostSocket

Answer:

b) ServerSocket

Explanation:

The ServerSocket class is used to create servers that listen for either local or remote client programs.

3. Which method is used to read data from an InputStream object?

a) readData()
b) pullData()
c) fetch()
d) read()

Answer:

d) read()

Explanation:

The read() method is used to read data from input streams.

4. What does the InetAddress class represent?

a) URL
b) IP Address
c) Host Name
d) Domain Name

Answer:

b) IP Address

Explanation:

The InetAddress class represents an IP address.

5. Which protocol ensures error-free data transmission?

a) HTTP
b) UDP
c) FTP
d) TCP

Answer:

d) TCP

Explanation:

TCP (Transmission Control Protocol) ensures error-free data transmission by establishing a connection and checking for lost packets.

6. Which class provides methods to work with URLs?

a) URLConnection
b) HttpURL
c) NetURL
d) URL

Answer:

d) URL

Explanation:

The URL class provides methods to work with Uniform Resource Locators.

7. Which exception is thrown when a connection cannot be established with a remote server?

a) IOException
b) ConnectionException
c) NetworkException
d) UnknownHostException

Answer:

d) UnknownHostException

Explanation:

UnknownHostException is thrown if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.

8. What is the default port number for HTTP?

a) 25
b) 8080
c) 21
d) 80

Answer:

d) 80

Explanation:

The default port for HTTP is 80.

9. Which class provides methods to create a client-side socket in Java?

a) ServerSocket
b) NetSocket
c) Socket
d) ClientSocket

Answer:

c) Socket

Explanation:

The Socket class in the java.net package is used to create client-side sockets.

10. Which Java class represents a socket address, consisting of an IP and port number?

a) InetAddress
b) InetPort
c) SocketAddress
d) InetSocketAddress

Answer:

d) InetSocketAddress

Explanation:

InetSocketAddress is a class that represents a socket address with an IP address and a port number.

11. Which class is used to create datagram packets?

a) DatagramSocket
b) DatagramPacket
c) UDPDatagram
d) UDPPacket

Answer:

b) DatagramPacket

Explanation:

The DatagramPacket class is used to create datagram packets to be sent or received using UDP.

12. Which of the following is NOT a transport layer protocol?

a) TCP
b) UDP
c) FTP
d) SCTP

Answer:

c) FTP

Explanation:

FTP stands for File Transfer Protocol and is an application layer protocol. TCP, UDP, and SCTP are all transport layer protocols.

13. What is the purpose of the bind() method in the ServerSocket class?

a) To close the connection
b) To write data to the socket
c) To connect to a client
d) To bind to a specific port number

Answer:

d) To bind to a specific port number

Explanation:

The bind() method in the ServerSocket class is used to bind the server to a specific port number.

14. Which class is used to create an HTTP connection to a URL?

a) HttpURLConnection
b) HttpSocket
c) HttpClient
d) URLConnection

Answer:

a) HttpURLConnection

Explanation:

HttpURLConnection is a subclass of URLConnection and provides methods to create an HTTP connection to a URL.

15. Which exception is thrown when a socket-related error occurs?

a) NetworkException
b) SocketException
c) IOException
d) ConnectException

Answer:

b) SocketException

Explanation:

While various exceptions can occur during networking tasks, SocketException is a common exception thrown for socket-related errors. Note that SocketException is a subclass of IOException.

16. Which method is used to get the output stream of a socket?

a) getOutputStream()
b) writeStream()
c) sendStream()
d) fetchOutputStream()

Answer:

a) getOutputStream()

Explanation:

The getOutputStream() method of the Socket class is used to retrieve the output stream of the socket.

17. What does URL stand for?

a) Uniform Resource Locator
b) Unified Retrieval Language
c) Universal Resource Link
d) Uniform Retrieval Link

Answer:

a) Uniform Resource Locator

Explanation:

URL stands for Uniform Resource Locator, which is used to specify addresses on the World Wide Web.

18. What is the primary difference between TCP and UDP?

a) TCP is faster than UDP
b) UDP is connection-oriented while TCP is connection-less
c) TCP is connection-oriented while UDP is connection-less
d) Both TCP and UDP ensure error-free data transmission

Answer:

c) TCP is connection-oriented while UDP is connection-less

Explanation:

TCP establishes a connection before sending data, ensuring that all data packets arrive and are in order. UDP sends packets without setting up a connection and doesn't guarantee delivery or order.

19. Which method of the ServerSocket class is used to listen to client requests?

a) accept()
b) listen()
c) connect()
d) request()

Answer:

a) accept()

Explanation:

The accept() method of the ServerSocket class listens for client requests and establishes a connection when a client request is received.

20. Which of the following is true about a Datagram socket?

a) It uses TCP for communication
b) It is connection-oriented
c) It is used for sending and receiving data in packets
d) It requires a handshake before data transfer

Answer:

c) It is used for sending and receiving data in packets

Explanation:

Datagram sockets use the UDP protocol (which is connectionless) to send and receive data in packets, without the need for an initial handshake or maintaining a persistent connection.


Comments