MCQs on Networking in Java | Java Networking

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

  1. 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
  2. Which class in Java is used to implement the client-side socket?
    • a) Socket
    • b) ServerSocket
    • c) DatagramSocket
    • d) URLConnection
  3. Which method in the Socket class connects to a server?
    • a) connect()
    • b) open()
    • c) establish()
    • d) create()
  4. 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
  5. Which Java class is used for both client and server communication using TCP?
    • a) Socket
    • b) DatagramSocket
    • c) URL
    • d) URLConnection

Client-Server Communication

  1. 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
  2. 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.
  3. 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
  4. 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
  5. 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

  1. Which type of socket is used for connectionless communication in Java?
    • a) TCP socket
    • b) UDP socket
    • c) Multicast socket
    • d) HTTP socket
  2. What does the DatagramSocket class use for communication in Java?
    • a) TCP
    • b) UDP
    • c) HTTP
    • d) FTP
  3. 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
  4. What class is used to represent a packet of data in a DatagramSocket communication?
    • a) DatagramPacket
    • b) PacketData
    • c) DataPacket
    • d) MessagePacket
  5. 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

  1. Which class in Java is used to work with URLs?
    • a) URLConnection
    • b) Socket
    • c) URL
    • d) HttpURLConnection
  2. 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
  3. Which HTTP method is used to retrieve data from a specified URL in Java?
    • a) POST
    • b) GET
    • c) PUT
    • d) DELETE
  4. 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.
  5. 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

  1. What class is used to open a TCP connection to a server in Java?
    • a) Socket
    • b) DatagramSocket
    • c) URLConnection
    • d) ServerSocket
  2. 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.
  3. 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.
  4. 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
  5. Which of the following is used to represent the client-side socket in Java?
    • a) Socket
    • b) ServerSocket
    • c) DatagramSocket
    • d) URL
  6. Which method is used to connect a Socket object to a remote server in Java?
    • a) connect()
    • b) bind()
    • c) getInputStream()
    • d) listen()
  7. What is the default port number for HTTP communication in Java?
    • a) 8080
    • b) 80
    • c) 443
    • d) 25
  8. 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.
  9. Which of the following methods is used to send data to a URL in Java?
    • a) getOutputStream()
    • b) getInputStream()
    • c) openConnection()
    • d) connect()
  10. 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

QnoAnswer
1a) A network connection point
2a) Socket
3a) connect()
4b) Listens for incoming client connections
5a) Socket
6a) Socket
7b) ServerSocket listens for incoming client requests, while Socket is used to create a client connection.
8a) By the IP address and port number
9a) Creating a ServerSocket object
10a) getOutputStream()
11b) UDP socket
12b) UDP
13c) Faster transmission with no connection setup
14a) DatagramPacket
15a) It binds the socket to a specific port number.
16c) URL
17c) To read and write data to and from a URL
18b) GET
19a) By creating an HttpURLConnection object and using the getOutputStream() method.
20a) getInputStream()
21a) Socket
22b) It can be used for both client-side and server-side communication.
23b) It is used to write data to the server from the client.
24c) By using TCP sockets
25a) Socket
26a) connect()
27b) 80
28b) Sets the method type (GET, POST, etc.) for the HTTP request.
29a) getOutputStream()
30c) It returns the HTTP status code.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top