programming beginner 10 sample questions

Go (golang) MCQ Practice Test

A statically typed, compiled language designed at Google for simplicity and concurrency.

Q1. In Go, what does 'for i, v := range mySlice' do?

Explanation: range in a for loop iterates over slices (index, value), maps (key, value), channels, strings (rune indices), and more.

Q2. What is the purpose of the & operator in Go?

Explanation: The & operator is used to get the memory address of a variable in Go. This is often used in conjunction with the * operator to create and use pointers.

Q3. What is the primary purpose of the 'error' return value (often assigned to a variable named 'err') when using functions from Go's built-in packages?

Explanation: In Go, many functions return an 'error' value as their second return value (or only return value if the function returns only one value). If the 'error' value is not nil, it indicates that an error occurred during the function's execution. The 'err' variable (or any variable name) is used to store this error value, allowing the programmer to check for and handle potential issues.

Q4. What is the purpose of the "defer" keyword in Go?

Explanation: The "defer" keyword in Go is used to delay the execution of a function until the surrounding function returns. This is useful for cleaning up resources such as file handles or database connections.

Q5. In Go, what is the purpose of the 'defer' statement in the following code snippet: 'defer fmt.Println('Hello World')'?

Explanation: The 'defer' statement in Go is used to specify a function to be executed when the surrounding function returns. This is often used for cleaning up resources, such as closing files or releasing locks.

Q6. What is the purpose of the 'go get' command in Go?

Explanation: The 'go get' command is used to download and install a Go package from the internet. It is a convenient way to fetch and install packages, making it easy to work with dependencies.

Q7. What is the purpose of the "go get" command in Go?

Explanation: The "go get" command is used to download and install a Go package from the internet. It is often used to obtain dependencies for a Go program.

Q8. In Go, what is the purpose of the `range` keyword in a `for` loop?

Explanation: The `range` keyword in Go is used in a `for` loop to iterate over various data structures. When used with maps, it iterates over keys and values. When used with arrays, slices, and strings, it iterates over indices and values (or runes for strings). The `range` keyword provides a convenient way to access elements within these data structures.

Q9. In Go, what operator is used for string concatenation?

Explanation: The '+' operator in Go is used for string concatenation. It combines two or more strings into a single string.

Q10. What is the purpose of the 'go mod init' command in Go?

Explanation: The 'go mod init' command is used to create a new Go module, which is a package that can be used as a dependency in other Go projects.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Go (golang) MCQ Practice Test | Mind Hustle