Java Questions & Answers – Networking – Datagrams

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Networking – Datagrams”.

1. Which of these is a bundle of information passed between machines?
a) Mime
b) Cache
c) Datagrams
d) DatagramSocket
View Answer

Answer: c
Explanation: The Datagrams are the bundle of information passed between machines.

2. Which of these class is necessary to implement datagrams?
a) DatagramPacket
b) DatagramSocket
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: Both DatagramPacket and DatagramSocket are essential for implementing datagram-based communication in Java. DatagramPacket represents the data being sent or received, while DatagramSocket is used to send and receive those packets over the network.

3. Which of these method of DatagramPacket is used to find the port number?
a) port()
b) getPort()
c) findPort()
d) recievePort()
View Answer

Answer: b
Explanation: The getPort() method returns the port number of the remote host to which the datagram packet is being sent or from which it was received.
advertisement

4. Which of these method of DatagramPacket is used to obtain the byte array of data contained in a datagram?
a) getData()
b) getBytes()
c) getArray()
d) recieveBytes()
View Answer

Answer: a
Explanation: The getData() method of the DatagramPacket class is used to retrieve the byte array of data contained in the datagram.

5. Which of these methods of DatagramPacket is used to find the length of byte array?
a) getnumber()
b) length()
c) Length()
d) getLength()
View Answer

Answer: d
Explanation: The method getLength() is used to find the length of the byte array in a DatagramPacket object. This method returns the length of the data contained in the packet.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

6. Which of these class must be used to send a datagram packets over a connection?
a) InetAdress
b) DatagramPacket
c) DatagramSocket
d) All of the mentioned
View Answer

Answer: d
Explanation: By using 5 classes we can send and receive data between client and server, these are InetAddress, Socket, ServerSocket, DatagramSocket, and DatagramPacket.

7. Which of these method of DatagramPacket class is used to find the destination address?
a) findAddress()
b) getAddress()
c) Address()
d) whois()
View Answer

Answer: b
Explanation: The getAddress() method returns the IP address of the machine to which the datagram is being sent or from which it was received.

8. Which of these is a return type of getAddress() method of DatagramPacket class?
a) DatagramPacket
b) DatagramSocket
c) InetAddress
d) ServerSocket
View Answer

Answer: c
Explanation: The return type of the getAddress() method of the DatagramPacket class is InetAddress. This method returns the IP address to which the packet is sent or from which it was received.

9. Which API gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
a) getSocketAddress()
b) getAddress()
c) address()
d) none of the mentioned
View Answer

Answer: a
Explanation: The getSocketAddress() method is used to retrieve the SocketAddress (typically consisting of the IP address and port number) of the remote host to which the datagram is being sent or from which it was received. Specifically, the DatagramPacket.getSocketAddress() method provides this information for the associated datagram packet.
advertisement

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.