Any2Quiz
Computer Science / Languages / Golang

Web Development Quiz - Languages Golang

Free practice quiz: Web Development (Languages Golang). 54 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 54 questions free

Sample questions

  1. In Go's `net/http` package, which function is commonly used to start an HTTP server?

    1. http.ListenAndServe()
    2. http.Serve()
    3. http.Handle()
    4. http.Register()
    Show answer

    Answer: A. http.ListenAndServe()

    http.ListenAndServe() is the primary function used to start an HTTP server, listening on a specified TCP address and then serving the handler to incoming requests.

  2. Which Go package is commonly used for building RESTful APIs with more advanced routing and middleware capabilities than `net/http` alone?

    1. gorilla/mux
    2. gin-gonic/gin
    3. go-chi/chi
    4. All of the above
    Show answer

    Answer: D. All of the above

    All `gorilla/mux`, `gin-gonic/gin`, and `go-chi/chi` are popular third-party frameworks that extend Go's standard `net/http` library with features like advanced routing, middleware support, request parsing, and more.

  3. When using `http.ListenAndServeTLS`, what are the required arguments?

    1. Address, certificate file path, and private key file path.
    2. Address, handler, and root CA path.
    3. Handler, server name, and port.
    4. Address, certificate content, and key content.
    Show answer

    Answer: A. Address, certificate file path, and private key file path.

    `http.ListenAndServeTLS(addr, certFile, keyFile, handler)` requires the network address, the path to the TLS certificate file, and the path to the private key file.

  4. Which of the following is true about Go's `http.ServeMux`?

    1. It performs exact path matching.
    2. It uses regular expressions for routing.
    3. It supports routing based on HTTP methods directly in registration.
    4. It only supports serving static files.
    Show answer

    Answer: A. It performs exact path matching.

    `http.ServeMux` matches patterns exactly. For example, `/a` will not match `/a/b`. More complex routing often requires third-party routers.

  5. When should you use `http.ResponseWriter.Write` vs. `http.ResponseWriter.WriteHeader`?

    1. `WriteHeader` sets the status code and headers; `Write` sends the response body. `WriteHeader` must be called before `Write`.
    2. `Write` sets the status code and headers; `WriteHeader` sends the response body.
    3. They are interchangeable and can be called in any order.
    4. `Write` is for text, `WriteHeader` is for binary data.
    Show answer

    Answer: A. `WriteHeader` sets the status code and headers; `Write` sends the response body. `WriteHeader` must be called before `Write`.

    The HTTP response consists of a status line, headers, and a body. `WriteHeader` writes the status line and headers. `Write` writes the body. Crucially, the first call to either `WriteHeader` or `Write` implicitly writes the status line and headers if they haven't been written yet, but `WriteHeader` provides explicit control over the status code.

Take the full quiz

More quizzes

Browse all Golang quizzes