Go Networking Fundamentals: TCP and UDP Basics Quiz Quiz

Explore key concepts in Go networking with this quiz covering TCP and UDP communications, socket handling, connection methods, and essential error handling techniques. Designed for Go beginners, this quiz helps reinforce knowledge of core principles needed for basic network programming.

  1. Go TCP Connection Basics

    Which function is used in Go to create a TCP client connection to a remote server?

    1. net.Accept
    2. net.Bind
    3. net.Dial
    4. net.Listen

    Explanation: The net.Dial function in Go is used for creating a client-side connection to a remote server using protocols like TCP or UDP. net.Listen is used for starting a server that listens for incoming connections. net.Bind is not a valid Go function, and net.Accept is used to accept incoming connections on the server side, not to create them.

  2. TCP vs UDP Protocols

    Which protocol should you use in Go for reliable, connection-oriented communication between two endpoints?

    1. ICMP
    2. HTTP
    3. UDP
    4. TCP

    Explanation: TCP is a connection-oriented protocol that ensures reliable and ordered delivery of data between two endpoints. UDP is connectionless and does not guarantee delivery or order. ICMP is used for network diagnostics, not regular data transfer. HTTP is an application-level protocol that typically runs over TCP.

  3. Creating a UDP Socket

    In Go, which function do you use to create a UDP socket for sending or receiving messages?

    1. net.AcceptUDP
    2. net.ListenUDP
    3. net.BindTCP
    4. net.ConnectTCP

    Explanation: net.ListenUDP is used in Go to create a UDP socket for sending and receiving datagrams. net.ConnectTCP and net.BindTCP are not valid Go functions. net.AcceptUDP does not exist, as UDP is connectionless and does not accept connections like TCP.

  4. Reading Data from a TCP Connection

    After establishing a TCP connection in Go, which method is commonly used to read data from the connection?

    1. Read
    2. Fetch
    3. Send
    4. Transmit

    Explanation: The Read method is used to receive data from a TCP connection in Go. Send, Transmit, and Fetch are not standard Go methods for reading data from network connections. Using Read allows you to access incoming bytes from the connection efficiently.

  5. Error Handling in Go Networking

    What is the recommended way to handle network errors in Go after attempting to connect to a remote server?

    1. Rely on automatic retries
    2. Print a warning without checking
    3. Ignore the error
    4. Check the returned error value

    Explanation: In Go, it is best practice to always check the error value returned by networking functions to handle problems such as connection failures. Automatic retries are not performed by default, ignoring errors can lead to unexpected behavior, and printing a warning without handling does not resolve the underlying issue.

  6. Closing a Network Connection

    Which method should you call in Go to properly close a network connection after you're done with it?

    1. Disconnect
    2. Terminate
    3. Close
    4. End

    Explanation: The Close method is used to properly close open network connections in Go, signaling the end of communication and freeing resources. Disconnect, End, and Terminate are not valid Go connection methods; failing to use Close may leave the connection open and cause resource leaks.

  7. Listening for TCP Connections

    What function is used in Go to start a server that listens for incoming TCP connections on a specific port?

    1. net.Setup
    2. net.DialTCP
    3. net.AcceptListen
    4. net.Listen

    Explanation: net.Listen is the function used to start listening for incoming TCP connections on a specified port. net.DialTCP is used for making outbound TCP connections. net.AcceptListen and net.Setup are not valid Go networking functions.

  8. UDP Datagram Sending

    Which method allows you to send a UDP packet to a specified address in Go using a UDP connection object?

    1. Transfer
    2. SendTo
    3. WriteTo
    4. PushData

    Explanation: WriteTo is used in Go to send a UDP datagram to a particular address using a UDP connection object. SendTo is not a standard Go method, PushData and Transfer are not valid UDP methods in Go, so they would not work for sending UDP packets.

  9. Distinguishing TCP Sockets in Go

    When using net.Listen in Go to create a TCP server, what type of object do you typically receive to accept new connections?

    1. Listener
    2. Accepter
    3. Socket
    4. Connector

    Explanation: net.Listen returns a Listener object in Go, which is used to accept new incoming connections. Connector, Accepter, and Socket are incorrect; there is no standard type by those names provided for TCP listening in Go’s net package.

  10. Port Numbers in Go Networking

    What is the valid range of port numbers that can be used when setting up a TCP or UDP server in Go?

    1. 1 to 32767
    2. 1 to 1024
    3. 1000 to 65535
    4. 0 to 65535

    Explanation: The valid port number range is from 0 to 65535. Ports 1 to 1024 are known as well-known ports, but higher ports are also commonly used. 1 to 32767 and 1000 to 65535 only cover partial ranges, so the complete correct range is 0 to 65535.