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 freeSample questions
In Go's `net/http` package, which function is commonly used to start an HTTP server?
- http.ListenAndServe()
- http.Serve()
- http.Handle()
- 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.
Which Go package is commonly used for building RESTful APIs with more advanced routing and middleware capabilities than `net/http` alone?
- gorilla/mux
- gin-gonic/gin
- go-chi/chi
- 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.
When using `http.ListenAndServeTLS`, what are the required arguments?
- Address, certificate file path, and private key file path.
- Address, handler, and root CA path.
- Handler, server name, and port.
- 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.
Which of the following is true about Go's `http.ServeMux`?
- It performs exact path matching.
- It uses regular expressions for routing.
- It supports routing based on HTTP methods directly in registration.
- 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.
When should you use `http.ResponseWriter.Write` vs. `http.ResponseWriter.WriteHeader`?
- `WriteHeader` sets the status code and headers; `Write` sends the response body. `WriteHeader` must be called before `Write`.
- `Write` sets the status code and headers; `WriteHeader` sends the response body.
- They are interchangeable and can be called in any order.
- `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.