MCQs on Go Modules and Packages | Go

Learn the essentials of Go modules and packages, including how to create and manage modules, import custom and standard packages, and understand package organization and conventions for efficient Go programming.


Creating and Managing Modules

  1. What command is used to initialize a new Go module?
    a) go init
    b) go start
    c) go mod init
    d) go create module
  2. What file does Go create when you initialize a module using go mod init?
    a) go.mod
    b) go.config
    c) go.package
    d) go.setup
  3. Which of the following is used to download a module’s dependencies?
    a) go get
    b) go fetch
    c) go install
    d) go modules
  4. What command can be used to update the dependencies listed in the go.mod file?
    a) go mod update
    b) go get -u
    c) go install
    d) go mod fix
  5. What command removes unused dependencies from the go.mod file?
    a) go mod tidy
    b) go mod prune
    c) go mod clear
    d) go clean
  6. What is the purpose of the go.sum file?
    a) To store metadata about the module
    b) To track the versions of modules and their dependencies
    c) To list all the available functions in a module
    d) To store module installation paths
  7. How do you download a specific version of a module?
    a) go mod get <module>@<version>
    b) go fetch <module>@<version>
    c) go install <module>@<version>
    d) go update <module>@<version>
  8. What command helps to check if all the dependencies are correctly fetched and available?
    a) go mod check
    b) go mod verify
    c) go mod install
    d) go get
  9. Which command allows you to list all the available modules for a Go project?
    a) go list
    b) go show
    c) go mod list
    d) go modules
  10. What happens when you run go mod tidy?
    a) It removes unnecessary files from the module
    b) It downloads all dependencies
    c) It removes unused dependencies from go.mod
    d) It compiles the Go program

Importing and Using Custom and Standard Packages

  1. What is the correct syntax to import a package in Go?
    a) import <package>
    b) import "<package>"
    c) include "<package>"
    d) use "<package>"
  2. Which of these is an example of importing a custom package?
    a) import "math"
    b) import "fmt"
    c) import "myproject/utils"
    d) import <myproject/utils>
  3. Which Go standard package is used to format input and output?
    a) math
    b) fmt
    c) strings
    d) os
  4. How can you import multiple packages in Go?
    a) import ("fmt" "math")
    b) import ("fmt", "math")
    c) import [fmt, math]
    d) import "fmt", "math"
  5. What is the best practice when importing packages in Go?
    a) Only import the packages that are directly used in the code
    b) Always import all available packages
    c) Import packages in alphabetical order
    d) Import packages with full paths only
  6. How can you give an alias to an imported package?
    a) import math as m
    b) import "math" as m
    c) import m "math"
    d) import m := "math"
  7. What is the purpose of the blank identifier (_) when importing packages?
    a) It imports a package without using it in the program
    b) It imports a package and makes it accessible globally
    c) It skips the initialization of the package
    d) It imports a package but suppresses its functions
  8. In Go, which of these functions is part of the strings package?
    a) Join
    b) Print
    c) Create
    d) Append
  9. What is the command used to update the dependencies for a Go module?
    a) go install
    b) go update
    c) go mod update
    d) go get
  10. How do you check the documentation for an imported package in Go?
    a) go doc <package>
    b) go help <package>
    c) go show <package>
    d) go list <package>

Package Organization and Conventions

  1. What is the recommended convention for naming a Go package?
    a) Lowercase, short, and descriptive names
    b) Camel case
    c) Snake case
    d) Uppercase, long names
  2. Where should Go source files be placed in a project?
    a) In the src directory
    b) In the bin directory
    c) In the go_modules directory
    d) In the root directory
  3. Which of the following is a common convention for organizing Go files in a project?
    a) Use the main.go file for all program logic
    b) Place each function in a separate file
    c) Organize code by functionality into different directories
    d) Store all source files in one directory
  4. What does the main package indicate in Go?
    a) It is used for creating libraries
    b) It is the entry point of a Go application
    c) It is a standard library package
    d) It is used for unit testing
  5. Where should tests be placed in a Go project?
    a) In the test folder
    b) In the same directory as the source files with a .go extension
    c) In a separate repository
    d) In the bin folder
  6. How do you define a Go package in a source file?
    a) package <name>
    b) package <project>
    c) define <package>
    d) set package <name>
  7. Which command is used to format Go code according to Go conventions?
    a) go fmt
    b) go format
    c) go style
    d) go pretty
  8. What is the purpose of the go test command?
    a) To run the Go application
    b) To test if Go is installed correctly
    c) To run the unit tests for the Go program
    d) To build the Go program
  9. What should be included in the README.md file of a Go project?
    a) Instructions for compiling and running the project
    b) A list of Go functions
    c) A list of external modules
    d) A list of errors in the code
  10. Which directory convention is often used in Go to store documentation?
    a) /docs
    b) /readme
    c) /doc
    d) /src

Answer Key

QNoAnswer (Option with text)
1c) go mod init
2a) go.mod
3a) go get
4b) go get -u
5a) go mod tidy
6b) To track the versions of modules and their dependencies
7a) go mod get <module>@<version>
8b) go mod verify
9c) go mod list
10c) It removes unused dependencies from go.mod
11b) import "<package>"
12c) import "myproject/utils"
13b) fmt
14a) import ("fmt" "math")
15a) Only import the packages that are directly used in the code
16c) import m "math"
17a) It imports a package without using it in the program
18a) Join
19d) go get
20a) go doc <package>
21a) Lowercase, short, and descriptive names
22a) In the src directory
23c) Organize code by functionality into different directories
24b) It is the entry point of a Go application
25b) In the same directory as the source files with a .go extension
26a) package <name>
27a) go fmt
28c) To run the unit tests for the Go program
29a) Instructions for compiling and running the project
30a) /docs

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