MCQs on Building RESTful APIs | Go

Master Building RESTful APIs in Go with this extensive quiz. Learn how to structure your APIs, implement middleware for authentication and logging, and connect with databases using Gorm or SQLx.


MCQs on Building RESTful APIs in Go

Structuring REST APIs in Go

  1. What is the primary purpose of the net/http package in Go when building a RESTful API?
    a) It provides functions for manipulating files
    b) It allows you to manage HTTP requests and responses
    c) It handles JSON serialization
    d) It is used for routing in web applications
  2. In Go, which function is used to start an HTTP server?
    a) StartServer()
    b) ListenAndServe()
    c) BeginServer()
    d) RunServer()
  3. What is the role of a router in a Go REST API?
    a) To handle HTTP requests and route them to appropriate handlers
    b) To manage the database connections
    c) To handle authentication
    d) To process JSON responses
  4. What does the http.HandleFunc() method do in Go?
    a) It serves static files from a given directory
    b) It routes an HTTP request to a specific function
    c) It configures the database connection
    d) It validates incoming requests
  5. In Go, what is the recommended way to return a JSON response in a REST API handler?
    a) Use the json.Marshal() function to convert data into JSON format
    b) Send the data directly as a string
    c) Return data in XML format
    d) Use the http.Write() function to send data
  6. How do you define a custom API endpoint in Go?
    a) By creating a new file
    b) By writing a function and associating it with a route
    c) By calling the createEndpoint() function
    d) By manually setting the HTTP method
  7. What is the purpose of the http.ListenAndServe() function in Go?
    a) To start the API server and listen for incoming HTTP requests
    b) To handle database queries
    c) To start a Goroutine
    d) To validate incoming requests
  8. Which Go package is commonly used to parse URL parameters in a REST API request?
    a) encoding/json
    b) net/http
    c) github.com/gorilla/mux
    d) strings
  9. What is a common Go convention for versioning REST APIs?
    a) Including the version number in the domain name
    b) Including the version number in the URL path
    c) Including the version number in the HTTP header
    d) Using the v query parameter
  10. What does the http.NewServeMux() function do in Go?
    a) It creates a new HTTP server instance
    b) It creates a new multiplexer for routing requests
    c) It handles HTTP responses
    d) It processes incoming JSON data

Middleware for Authentication and Logging

  1. What is middleware in the context of a Go REST API?
    a) A database management tool
    b) A piece of code that processes HTTP requests before they reach the handler
    c) A function that validates API responses
    d) A library for serving static files
  2. Which HTTP method is commonly used for authentication in a RESTful API?
    a) POST
    b) PUT
    c) GET
    d) DELETE
  3. In Go, how do you create a middleware function?
    a) By defining a function that takes an http.Request and http.ResponseWriter
    b) By using http.HandleFunc()
    c) By importing an external middleware library
    d) By using the mux.NewRouter() function
  4. What is the primary role of authentication middleware in a REST API?
    a) To check if the user is authorized to access a resource
    b) To log the API requests
    c) To handle API errors
    d) To parse JSON data
  5. Which Go function is commonly used for logging HTTP requests in middleware?
    a) log.Fatal()
    b) fmt.Println()
    c) log.Printf()
    d) http.Log()
  6. In Go, how do you ensure middleware is applied to an API route?
    a) By passing the middleware function as an argument to the route handler
    b) By manually invoking the middleware in each handler function
    c) By using the http.HandleFunc() function
    d) By adding the middleware to the main() function
  7. Which of the following is a common method to implement token-based authentication in Go?
    a) Using a session cookie
    b) Using an API key passed in the header
    c) Using JWT tokens in the authorization header
    d) Using OAuth2 tokens in the query string
  8. How does logging middleware benefit a Go REST API?
    a) It ensures all requests are logged for debugging and analysis
    b) It automatically formats HTTP responses
    c) It limits the rate of requests
    d) It validates the input data in each request
  9. What is the purpose of the http.Request object in Go’s middleware?
    a) To store the HTTP response
    b) To represent the incoming HTTP request
    c) To manage the database connection
    d) To manage the router configuration
  10. How do you handle authentication failures in Go middleware?
    a) By returning a custom error message and an HTTP 401 status code
    b) By logging the error and continuing the request
    c) By sending a 500 error code
    d) By automatically retrying the request

Connecting with Databases using Gorm or SQLx

  1. What is Gorm in Go?
    a) A library for handling HTTP requests
    b) A tool for managing user authentication
    c) A popular ORM (Object-Relational Mapping) library for Go
    d) A tool for structuring Go APIs
  2. How do you connect to a database using Gorm in Go?
    a) gorm.Connect()
    b) gorm.Open()
    c) database.Connect()
    d) gorm.Database()
  3. Which Go package is commonly used to work with SQL databases in a more flexible way than the standard database/sql package?
    a) github.com/gorilla/mux
    b) github.com/jmoiron/sqlx
    c) github.com/go-sql-driver/mysql
    d) github.com/gin-gonic/gin
  4. How do you perform a query with Gorm in Go?
    a) db.Query()
    b) db.Select()
    c) db.Find()
    d) db.Execute()
  5. Which of the following is used to create database tables with Gorm?
    a) db.Migrate()
    b) db.CreateTable()
    c) db.AutoMigrate()
    d) db.NewTable()
  6. What is SQLx in Go used for?
    a) For managing HTTP middleware
    b) For working with SQL databases more easily than using database/sql
    c) For logging SQL queries
    d) For serializing SQL data into JSON format
  7. What is the first step to use Gorm in a Go application?
    a) Create a new SQL database
    b) Import the Gorm package
    c) Write SQL queries
    d) Create a middleware for authentication
  8. What does Gorm’s AutoMigrate() method do?
    a) It automatically updates the database schema based on model changes
    b) It migrates data from one database to another
    c) It migrates data to a backup server
    d) It handles user authentication
  9. Which Go package is needed to work with PostgreSQL databases using Gorm?
    a) github.com/go-sql-driver/mysql
    b) github.com/lib/pq
    c) github.com/jmoiron/sqlx
    d) github.com/gorilla/mux
  10. How do you retrieve data from a database using SQLx in Go?
    a) db.QueryRow()
    b) db.Get()
    c) db.Find()
    d) db.Select()

Answers Table

QnoAnswer
1b) It allows you to manage HTTP requests and responses
2b) ListenAndServe()
3a) To handle HTTP requests and route them to appropriate handlers
4b) It routes an HTTP request to a specific function
5a) Use the json.Marshal() function to convert data into JSON format
6b) By writing a function and associating it with a route
7a) To start the API server and listen for incoming HTTP requests
8c) github.com/gorilla/mux
9b) Including the version number in the URL path
10b) It creates a new multiplexer for routing requests
11b) A piece of code that processes HTTP requests before they reach the handler
12a) POST
13a) By defining a function that takes an http.Request and http.ResponseWriter
14a) To check if the user is authorized to access a resource
15c) log.Printf()
16a) By passing the middleware function as an argument to the route handler
17c) Using JWT tokens in the authorization header
18a) It ensures all requests are logged for debugging and analysis
19b) To represent the incoming HTTP request
20a) By returning a custom error message and an HTTP 401 status code
21c) A popular ORM (Object-Relational Mapping) library for Go
22b) gorm.Open()
23b) github.com/jmoiron/sqlx
24c) db.Find()
25c) db.AutoMigrate()
26b) For working with SQL databases more easily than using database/sql
27b) Import the Gorm package
28a) It automatically updates the database schema based on model changes
29b) github.com/lib/pq
30b) db.Get()

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