MCQs on Web Programming with Go | Go

Web programming with Go involves building HTTP servers, handling routes, middleware, and serving static files. Test your understanding of Go’s web frameworks, networking, and server-side programming with these MCQs.


Building HTTP Servers with net/http

  1. How do you import the Go package for building HTTP servers?
    a) import "http"
    b) import "net/http"
    c) import "web/http"
    d) import "server/http"
  2. Which function is used to create a basic HTTP server in Go?
    a) http.ListenAndServe()
    b) http.NewServer()
    c) http.CreateServer()
    d) http.Start()
  3. What does the http.HandleFunc() function do in Go?
    a) It creates a new HTTP request handler
    b) It defines a URL path and associates it with a function
    c) It starts the HTTP server
    d) It handles incoming HTTP requests
  4. Which method is used to listen on a specific port in Go’s http package?
    a) http.Listen()
    b) http.ListenAndServe()
    c) http.NewListener()
    d) http.StartServer()
  5. In Go, which function handles HTTP requests asynchronously?
    a) http.ListenAndServe()
    b) http.Handle()
    c) http.RequestHandler()
    d) http.HandleFunc()
  6. What is the purpose of the http.ResponseWriter parameter in Go’s http.HandleFunc()?
    a) To process incoming requests
    b) To write the HTTP response to the client
    c) To define HTTP status codes
    d) To log HTTP requests
  7. Which function is used to send an HTTP response in Go?
    a) http.SendResponse()
    b) http.Write()
    c) response.Write()
    d) response.WriteHeader()
  8. What does http.ListenAndServe(":8080", nil) do in Go?
    a) It starts the server on port 8080 with no handler
    b) It starts the server on port 8080 with the default handler
    c) It binds the server to port 8080
    d) It listens for connections on port 8080 but does not start the server
  9. Which method in Go is used to handle HTTP requests for a specific URL?
    a) http.Handle()
    b) http.HandleFunc()
    c) http.ListenAndServe()
    d) http.Request()
  10. What does the http.NewServeMux() function do in Go?
    a) It creates a new HTTP server
    b) It creates a new router for handling HTTP requests
    c) It initializes the HTTP server
    d) It listens for incoming HTTP requests

Handling Routes and Middleware

  1. How do you define a custom route in Go using the net/http package?
    a) http.Handle("/route", handler)
    b) http.Route("/route", handler)
    c) http.NewRoute("/route", handler)
    d) http.HandleFunc("/route", handler)
  2. What is the purpose of middleware in Go’s HTTP server?
    a) To modify HTTP requests before they reach the handler
    b) To route the request to an appropriate handler
    c) To manage HTTP status codes
    d) To log the request data
  3. Which is the most common approach to implementing middleware in Go?
    a) By using http.HandleFunc()
    b) By wrapping the handler function
    c) By using http.Route()
    d) By manually modifying HTTP responses
  4. What does the http.ServeHTTP() method do?
    a) It starts the HTTP server
    b) It dispatches requests to the appropriate handler
    c) It listens for incoming requests
    d) It processes and handles the HTTP request
  5. Which Go function is typically used to create middleware that logs HTTP requests?
    a) log.Request()
    b) http.LogRequest()
    c) logHTTP()
    d) Custom handler function wrapping the main handler
  6. What is the role of the http.Request object in Go web programming?
    a) It contains details about the HTTP response
    b) It holds information about the incoming HTTP request
    c) It provides methods for sending responses
    d) It stores information about HTTP headers
  7. Which of the following is a valid use of a middleware in Go?
    a) Redirecting HTTP traffic
    b) Parsing HTTP body content
    c) Authentication and authorization checks
    d) All of the above
  8. How do you chain multiple middleware functions in Go?
    a) By using http.MiddlewareChain()
    b) By wrapping one middleware inside another
    c) By calling multiple http.Handle() functions
    d) By using http.MultiMiddleware()
  9. Which method is used to handle HTTP requests for specific routes with parameters in Go?
    a) http.Route()
    b) http.HandleFunc()
    c) http.Handle()
    d) http.RequestRoute()
  10. What does the http.Handle("/path", handler) function do in Go?
    a) It defines a URL path and associates it with a handler function
    b) It starts an HTTP server
    c) It listens for requests at the specified URL
    d) It serves the HTTP response

Serving Static Files

  1. Which Go function is used to serve static files like images or CSS?
    a) http.ServeFile()
    b) http.ServeStatic()
    c) http.Static()
    d) http.FileServe()
  2. How do you serve static files from a specific directory in Go?
    a) http.ServeFile("/path")
    b) http.ServeStatic("/path", "/static")
    c) http.ServeContent("/path", "/static")
    d) http.FileServer(http.Dir("/static"))
  3. Which function in Go returns an HTTP handler to serve files from a directory?
    a) http.ServeFile()
    b) http.ServeStatic()
    c) http.FileServer()
    d) http.StaticHandler()
  4. How do you restrict access to certain files while serving static content in Go?
    a) By setting permissions on the directory
    b) By using middleware to check for authorization
    c) By using custom HTTP handlers for specific file types
    d) Go does not support file access restrictions
  5. Which of the following best describes the http.FileServer() function in Go?
    a) It returns an HTTP handler that serves files from a specified directory
    b) It serves dynamic content like HTML pages
    c) It handles only images and multimedia content
    d) It creates a new HTTP server for each request
  6. How do you serve an HTML file using Go?
    a) Use http.ServeFile() with the file path
    b) Use http.ServeStatic() for the file path
    c) Use http.FileServe() to specify the file
    d) Use http.ServeContent() with an HTML file handler
  7. What would you use http.ServeFile() for in Go?
    a) To send a file as the HTTP response
    b) To send dynamic data in the response
    c) To start a web server
    d) To authenticate requests for file access
  8. Can Go serve files with custom headers, such as caching headers?
    a) Yes, by using custom http.ServeHTTP() handlers
    b) No, Go serves files with default headers only
    c) Yes, by modifying the http.FileServer() function
    d) No, Go cannot serve static files with custom headers
  9. How do you handle requests for nonexistent static files in Go?
    a) By using middleware to return a 404 error
    b) By setting a default fallback route
    c) Go automatically returns a 404 for missing files
    d) Both a and b
  10. How can you configure Go to serve static files from multiple directories?
    a) By chaining multiple http.FileServer() handlers
    b) By modifying the http.ServeFile() function
    c) By using a custom file path handler
    d) Go does not support serving files from multiple directories

Answer Key

QnoAnswer
1b) import "net/http"
2a) http.ListenAndServe()
3b) It defines a URL path and associates it with a function
4b) http.ListenAndServe()
5d) http.HandleFunc()
6b) To write the HTTP response to the client
7c) response.Write()
8b) It starts the server on port 8080 with the default handler
9b) http.HandleFunc()
10b) It creates a new router for handling HTTP requests
11d) http.HandleFunc("/route", handler)
12a) To modify HTTP requests before they reach the handler
13b) By wrapping the handler function
14b) It dispatches requests to the appropriate handler
15d) Custom handler function wrapping the main handler
16b) It holds information about the incoming HTTP request
17d) All of the above
18b) By wrapping one middleware inside another
19b) http.HandleFunc()
20a) It defines a URL path and associates it with a handler function
21a) http.ServeFile()
22d) http.FileServer(http.Dir("/static"))
23c) http.FileServer()
24b) By using middleware to check for authorization
25a) It returns an HTTP handler that serves files from a specified directory
26a) Use http.ServeFile() with the file path
27a) To send a file as the HTTP response
28a) Yes, by using custom http.ServeHTTP() handlers
29d) Both a and b
30a) By chaining multiple http.FileServer() handlers

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