Java Networking MCQ

Networking in Java enables the development of network applications where multiple systems can communicate with each other. The java.net package provides support for both clients and servers, allowing the creation and management of sockets, reading and writing data over network connections, and more. Let's dive into a quiz to assess your understanding of Java networking basics!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. 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.

2. 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.

3. Which class in Java is used to create a server-side socket?

a) Server
b) SocketServer
c) ServerNet
d) ServerSocket

Answer:

d) ServerSocket

Explanation:

The ServerSocket class is used to create server-side sockets that can listen for and accept connections from clients.

4. In which package are most of the networking classes/interfaces defined?

a) java.network
b) java.net
c) java.socket
d) java.connect

Answer:

b) java.net

Explanation:

The java.net package contains most of the classes and interfaces for networking in Java.

5. 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.

6. Which method is used to read from an input stream connected to a socket?

a) readStream()
b) read()
c) socketRead()
d) inputRead()

Answer:

b) read()

Explanation:

The read() method of InputStream is used to read data from the input stream connected to a socket.

7. Which class provides method(s) to retrieve IP addresses of a given host?

a) Socket
b) Host
c) InetAddress
d) NetworkAddress

Answer:

c) InetAddress

Explanation:

The InetAddress class provides methods to retrieve IP addresses, like getByName() and getLocalHost().

8. 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.

9. Which class is used to perform DNS lookup in Java?

a) DNSLookup
b) NetAddress
c) InetAddress
d) DomainLookup

Answer:

c) InetAddress

Explanation:

The InetAddress class provides methods like getByName() that can be used to perform DNS lookups to retrieve the IP address for a given host name.

10. Which method of the ServerSocket class is used to listen for 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.

Java offers a comprehensive toolkit for building network applications. Whether you're looking to develop a simple chat application or more complex network systems, understanding Java networking basics is key. If you found this quiz enlightening, keep exploring the extensive world of Java networking. Keep coding and keep learning!



Comments