Explore Java Networking MCQs to deepen your understanding of key networking concepts, such as Sockets, Client-Server communication, Datagram and TCP/IP Sockets, and working with URLs and HTTP in Java. Perfect for Java developers and interview preparation!
MCQs on Networking in Java
Introduction to Sockets
What is a socket in Java?
a) A network connection point
b) A buffer for storing data
c) A thread for handling communication
d) A type of object in Java
Which class in Java is used to implement the client-side socket?
a) Socket
b) ServerSocket
c) DatagramSocket
d) URLConnection
Which method in the Socket class connects to a server?
a) connect()
b) open()
c) establish()
d) create()
What does a ServerSocket object in Java do?
a) Connects to the server
b) Listens for incoming client connections
c) Sends data to the client
d) Performs encryption on the data
Which Java class is used for both client and server communication using TCP?
a) Socket
b) DatagramSocket
c) URL
d) URLConnection
Client-Server Communication
Which of the following classes is used by a client to send a request to a server in Java?
a) Socket
b) ServerSocket
c) URL
d) URLConnection
What is the main difference between the Socket and ServerSocket classes in Java?
a) Socket listens for connections, while ServerSocket connects to servers.
b) ServerSocket listens for incoming client requests, while Socket is used to create a client connection.
c) Socket is used for UDP, while ServerSocket is for TCP.
d) ServerSocket is used for broadcasting, while Socket is for unicast.
In client-server communication, how does the server know which client is sending a message?
a) By the IP address and port number
b) By the client’s machine name
c) By the server’s machine name
d) By the client’s MAC address
What is the first step in establishing a connection between a client and a server in Java?
a) Creating a ServerSocket object
b) Opening a socket on the client-side
c) Sending data to the server
d) Binding the socket to a specific port
Which of the following methods is used to send data from the client to the server using a Socket object?
a) getOutputStream()
b) writeData()
c) send()
d) transferData()
Datagram and TCP/IP Sockets
Which type of socket is used for connectionless communication in Java?
a) TCP socket
b) UDP socket
c) Multicast socket
d) HTTP socket
What does the DatagramSocket class use for communication in Java?
a) TCP
b) UDP
c) HTTP
d) FTP
What is the advantage of using UDP sockets over TCP sockets?
a) More secure communication
b) Reliable connection
c) Faster transmission with no connection setup
d) Guaranteed delivery of data
What class is used to represent a packet of data in a DatagramSocket communication?
a) DatagramPacket
b) PacketData
c) DataPacket
d) MessagePacket
What does the bind() method do in DatagramSocket?
a) It binds the socket to a specific port number.
b) It establishes a connection with a client.
c) It sends the data to the server.
d) It receives the data from a server.
URL and HTTP in Java
Which class in Java is used to work with URLs?
a) URLConnection
b) Socket
c) URL
d) HttpURLConnection
What is the main purpose of the URLConnection class in Java?
a) To create sockets for client-server communication
b) To connect a URL to a network protocol
c) To read and write data to and from a URL
d) To open a file in the system
Which HTTP method is used to retrieve data from a specified URL in Java?
a) POST
b) GET
c) PUT
d) DELETE
In Java, how do you send a request to a URL using HTTP?
a) By creating an HttpURLConnection object and using the getOutputStream() method.
b) By directly opening a socket to the URL.
c) By creating a URL object and calling the connect() method.
d) By creating a URLConnection and calling the getResponseCode() method.
What is the method used to read the response data from a URL in Java?
a) getInputStream()
b) readData()
c) getOutputStream()
d) connect()
Additional Questions on Java Networking
What class is used to open a TCP connection to a server in Java?
a) Socket
b) DatagramSocket
c) URLConnection
d) ServerSocket
Which of the following is true about Java’s Socket class?
a) It is used only for server-side communication.
b) It can be used for both client-side and server-side communication.
c) It can only handle UDP communication.
d) It establishes HTTP connections.
What is the role of OutputStream in Java socket programming?
a) It allows the server to accept incoming connections.
b) It is used to write data to the server from the client.
c) It establishes the client connection to the server.
d) It reads incoming messages from the server.
How do you ensure reliable communication in Java socket programming?
a) By using DatagramSocket
b) By using ServerSocket
c) By using TCP sockets
d) By using URL class
Which of the following is used to represent the client-side socket in Java?
a) Socket
b) ServerSocket
c) DatagramSocket
d) URL
Which method is used to connect a Socket object to a remote server in Java?
a) connect()
b) bind()
c) getInputStream()
d) listen()
What is the default port number for HTTP communication in Java?
a) 8080
b) 80
c) 443
d) 25
What does the setRequestMethod() method in HttpURLConnection do?
a) Sets the timeout duration for the request.
b) Sets the method type (GET, POST, etc.) for the HTTP request.
c) Sets the URL for the request.
d) Sets the user-agent header for the request.
Which of the following methods is used to send data to a URL in Java?
a) getOutputStream()
b) getInputStream()
c) openConnection()
d) connect()
What is the purpose of getResponseCode() in HttpURLConnection?
a) It reads the body of the response.
b) It checks the status of the connection.
c) It returns the HTTP status code.
d) It sets the request timeout.
Answer Table
Qno
Answer
1
a) A network connection point
2
a) Socket
3
a) connect()
4
b) Listens for incoming client connections
5
a) Socket
6
a) Socket
7
b) ServerSocket listens for incoming client requests, while Socket is used to create a client connection.
8
a) By the IP address and port number
9
a) Creating a ServerSocket object
10
a) getOutputStream()
11
b) UDP socket
12
b) UDP
13
c) Faster transmission with no connection setup
14
a) DatagramPacket
15
a) It binds the socket to a specific port number.
16
c) URL
17
c) To read and write data to and from a URL
18
b) GET
19
a) By creating an HttpURLConnection object and using the getOutputStream() method.
20
a) getInputStream()
21
a) Socket
22
b) It can be used for both client-side and server-side communication.
23
b) It is used to write data to the server from the client.
24
c) By using TCP sockets
25
a) Socket
26
a) connect()
27
b) 80
28
b) Sets the method type (GET, POST, etc.) for the HTTP request.