Go (Golang) Quiz - MCQ - Multiple Choice Questions

Welcome to the Go language quiz! This quiz consists of 25+ multiple-choice questions to put your knowledge to the test. These quiz questions cover a broad range of Golang topics, from basic syntax to more complex concepts.

You can view the answer and explanation for each question by clicking the "View Answer and Explanation" button.

1. What command is used to run a Go program? 

A. go run 
B. go start 
C. go exec 
D. go boot 

Answer: 

A. go run 

Explanation:

The go run command is used to compile and run a Go program. 

2. How can you format the Go source code in an idiomatic way? 

A. Using the gofmt tool 
B. Using the golint tool 
C. Using the gocode tool 
D. Using the goformat tool 

Answer: 

A. Using the gofmt tool 

Explanation:

The gofmt tool automatically formats Go source code in an idiomatic way, handling indents, spaces, and other formatting details. It's widely used in the Go community to maintain a consistent code style. It's worth noting that many Go IDEs and text editors will automatically run gofmt (or its sibling, goimports, which also adds and removes import lines as necessary) whenever you save a Go source file.

3. Which of the following is the correct way to declare a constant in Go? 

A. const PI = 3.14 
B. final PI = 3.14 
C. const float PI = 3.14 
D. constant PI = 3.14 

Answer: 

A. const PI = 3.14

Explanation:

The const keyword is used to declare a constant in Go. So, const PI = 3.14 is the correct way to declare a constant PI. 

4. What is the zero value for a string in Go? 

A. "" 
B. "0" 
C. null 
D. " " 

Answer: 

A. "" 

Explanation:

The zero value for a string in Go is an empty string (""), not null or "0". 

5. What is the correct syntax for a for loop in Go? 

A. for (i = 0; i < 10; i++) {} 
B. for i = 0; i < 10; i++ {} 
C. for i := 0; i < 10; i++ {} 
D. for i < 10; i++ {} 

Answer: 

C. for i := 0; i < 10; i++ {} 

Explanation:

In Go, you can declare and initialize a variable in the for loop itself. So, for i := 0; i < 10; i++ {} is the correct syntax for a for loop in Go. 

6. How do you write a single-line comment in Go? 

A. // comment 
B. # comment 
C. /* comment */ 
D. -- comment 

Answer: 

A. // comment 

Explanation:

In Go, single-line comments are created using //.

7. What is the keyword to define a function in Go? 

A. func 
B. function 
C. def 
D. define 

Answer: 

A. func 

Explanation:

The func keyword is used to define a function in Go. 

8. How do you define a multiline comment in Go? 

A. /** Comment */ 
B. # Comment # 
C. / Comment */ 
D. <!-- Comment --> 

Answer: 

C. / Comment */ 

Explanation:

Multiline comments in Go are enclosed within /* and */. 

9. What is the correct way to declare a slice in Go?

A. var a []int = {1, 2, 3} 
B. var a []int = [1, 2, 3] 
C. var a []int = []int{1, 2, 3} 
D. var a []int = slice{1, 2, 3} 

Answer: 

C. var a []int = []int{1, 2, 3}

Explanation:

In Go, a slice is declared with the []T syntax, where T is the type of elements in the slice. The []int{1, 2, 3} syntax initializes a slice with elements 1, 2, and 3. 

10. What does the range keyword do in Go?

A. It ranges over the elements in data structures like arrays, slices, strings, and maps. 
B. It generates a range of numbers. 
C. It checks if a number is within a specific range. 
D. It transforms a number to its range representation. 

Answer: 

A. It ranges over the elements in data structures like arrays, slices, strings, and maps. 

Explanation:

The range keyword is used in for loop to iterate over items of arrays, slices, strings, and maps. 

11. What does the len function do in Go? 

A. It calculates the length of a string. 
B. It calculates the length of a collection like an array, slice, or map. 
C. Both A and B. 
D. None of the above. 

Answer: 

C. Both A and B. 

Explanation:

The len function in Go can be used to get the length of a string or the number of elements in a collection such as an array, slice, or map. 

12. What keyword is used to create a new struct type in Go? 

A. struct 
B. new 
C. type 
D. create 

Answer: 

C. type 

Explanation:

The type keyword is used to define a new struct type in Go. 

13. Which keyword is used to stop a loop in Go? 

A. stop 
B. exit 
C. end 
D. break 

Answer: 

D. break 

Explanation:

The break keyword is used to stop a loop in Go.

14. Which of the following data types does Go not support? 

A. Float 
B. Char 
C. String 
D. Bool 

Answer: 

B. Char 

Explanation:

The Go doesn't have a dedicated character data type. Characters in Go are just runes (which are an alias for int32 type), and they can hold Unicode characters.

15. Which keyword is used to import packages in Go? 

A. import 
B. package 
C. require 
D. include 

Answer: 

A. import 

Explanation:

The import keyword is used to import packages in Go. 

16. Which of these statements is true about Go? 

A. Go does not support object-oriented programming. 
B. Go supports exception handling with try-and-catch blocks. 
C. Go does not have while or do while loops, it only has for loops. 
D. Go supports multithreading with the thread keyword. 

Answer: 

C. Go does not have while or do while loops, it only has for loops. 

Explanation:

Go does not have traditional while or do while loops, only the for loop, which can be used in a way similar to a while loop. As for the other options, Go supports a form of object-oriented programming (but not in the classical sense), does not use try/catch for error handling (instead, it returns errors as regular return values), and does not have a thread keyword (but it does support lightweight threads called goroutines).

17. How does Go handle unused variables? 

A. It ignores them. 
B. It throws a warning. 
C. It throws a compile-time error. 
D. It automatically removes them. 

Answer: 

C. It throws a compile-time error. 

Explanation:

In Go, unused variables result in a compile-time error. The language is designed to encourage clean code and doesn't allow variables to be declared without being used.

18. Which of these is NOT a Go built-in data type? 

A. String 
B. Tuple 
C. Boolean 
D. Integer 

Answer: 

B. Tuple

Explanation:

Tuple is not a built-in data type in Go. The built-in data types include numeric types (integer and floating-point), strings, and boolean.

19. What is the output of the following code?

package main
import "fmt"
func main() {
    var a = "Java"
    var b = "Guides"
    fmt.Println(a + b)
}
A. Java
B. Guides
C. JavaGuides
D. It will throw an error 

Answer: 

C. JavaGuides

Explanation:

The plus (+) operator is used to concatenate strings in Go. Hence, the output will be the concatenation of "Java" and "Guides", resulting in "JavaGuides".

20. What is the zero value for a boolean in Go? 

A. null 
B. 0 
C. false 
D. None 

Answer: 

C. false 

Explanation:

The zero value for a boolean in Go is false

21. In Go, what keyword is used to handle errors? 

A. try 
B. catch 
C. error 
D. defer

Answer: 

C. error 

Explanation:

The error type is a built-in interface used to represent and handle errors in Go.

22. What is the output of this program?

package main
import "fmt"
func main() {
    defer fmt.Println("World")
    fmt.Println("Hello")
}
A. Hello 
B. World 
C. Hello World 
D. World Hello 

Answer: 

C. Hello World 

Explanation:

The defer keyword in Go schedules a function call to be run after the function completes. Therefore, "Hello" will be printed first, then "World".

23. What will the following code print?

package main
import "fmt"
func main() {
    for i := 0; i < 5; i++ {
        if i%2 == 0 {
            continue
        }
        fmt.Println(i)
    }
}
A. 1 3 
B. 0 1 2 3 4 
C. 2 4 
D. 0 2 4 

Answer: 

A. 1 3 

Explanation:

The continue statement in Go skips the current iteration of the loop and continues with the next one. Hence, the program will print only the odd numbers (i.e., 1 and 3).

24. What is a goroutine in Go? 

A. A built-in function 
B. A type of loop 
C. A type of error 
D. A lightweight thread managed by the Go runtime 

Answer: 

D. A lightweight thread managed by the Go runtime 

Explanation:

A goroutine is a lightweight thread managed by the Go runtime. Goroutines allow you to run functions concurrently.

25. How can you define an anonymous function in Go? 

A. func() {} 
B. anonymous() {} 
C. func anonymous() {} 
D. You can't define an anonymous function in Go 

Answer: 

A. func() {} 

Explanation:

In Go, you can define an anonymous function with func() {}.

26. Which of these is NOT a rule for defining a package in Go? 

A. A package name should be in lowercase. 
B. A package name should not start with a number. 
C. A package name can include special characters like $ and _. 
D. A package should have a unique name to avoid name conflicts. 

Answer: 

C. A package name can include special characters like $ and _. 

Explanation:

A package name in Go can't include special characters like $ and _. 
Go has a few rules for package names: 
  • Package names should be all lowercase. 
  • Package names should not start with a number. 
  • The name should be descriptive and give an idea about the contents of the package. 
  • The name should be unique to avoid conflicts with other packages. 
  • Package names should avoid using Go's reserved words. 
In addition to these rules, Go encourages package names to be simple, short, and clear.

27. What do the strings.Join function do in Go? 

A. It joins two or more strings together. 
B. It checks if a string is contained in another string. 
C. It splits a string into a slice of substrings. 
D. It repeats a string a certain number of times. 

Answer: 

A. It joins two or more strings together. 

Explanation:

The strings.Join function in Go concatenates the elements of a slice of strings to create a single string. The elements are separated by the separator string provided as the second parameter. 

28. What is the result of cap([]int{1, 2, 3, 4, 5}) in Go? 

A. 1 
B. 3 
C. 5 
D. An error 

Answer: 

C. 5 

Explanation: 

The cap function returns the capacity of a slice. In this case, the capacity of the slice is 5. 

29. How can you append an element to a slice s? 

A. s.append(1) 
B. append(s, 1) 
C. s += 1 
D. append(1, s) 

Answer: 

B. append(s, 1) 

Explanation:

The append function in Go is used to add an element to a slice. The correct syntax is append(s, 1).

30. How does Go handle exceptions? 

A. With try/catch blocks 
B. With if/else statements 
C. With panic/recover mechanism 
D. With throw/catch blocks 

Answer: 

C. With panic/recover mechanism 

Explanation:

Go does not use the conventional exception-handling mechanisms found in languages like Java or Python. Instead, Go has a built-in mechanism for error handling using multiple return values and it also provides panic and recover for handling truly exceptional situations.

31. What is the role of the defer keyword in Go's error handling? 

A. It stops the execution of the function immediately.
B. It allows the execution of functions or methods to be delayed until the surrounding function returns. 
C. It throws an exception. 
D. It handles the error. 

Answer: 

B. It allows the execution of functions or methods to be delayed until the surrounding function returns. 

Explanation:

The defer keyword allows you to delay the execution of a statement until the surrounding function returns, either normally or through a panic. 

32. How can you recover from panic in Go? 

A. Using a try/catch block 
B. Using the recover() function in a deferred function 
C. By checking if the error is not nil 
D. Using the throw keyword 

Answer: 

B. Using the recover() function in a deferred function 

Explanation:

The recover function is a built-in function that regains control of a panicking goroutine. Recover is only useful inside deferred functions, where it retrieves the error value passed to the panic call. 

33. What does the panic function do in Go? 

A. It returns an error if one occurs. 
B. It stops the ordinary flow of a goroutine. 
C. It catches an exception. 
D. It throws an exception. 

Answer: 

B. It stops the ordinary flow of a goroutine. 

Explanation:

The panic function stops the ordinary flow of control and begins panicking. When the function F calls panic, execution of F stops, and any deferred functions in F are executed normally.

34. Which of the following statements about the string data type in Go is true? 

A. String values can be changed once they are created. 
B. Strings in Go are arrays of characters. 
C. Strings in Go are a sequence of Unicode code points. 
D. Strings in Go are not a basic data type. 

Answer: 

C. Strings in Go are a sequence of Unicode code points. 

Explanation:

In Go, a string is a sequence of bytes, not characters, and these bytes usually represent Unicode code points. This means that a single 'character' could be represented by one or more bytes. Furthermore, strings in Go are immutable, meaning that they cannot be changed once they are created. This is different from some other programming languages, like C++, where strings are just arrays of characters that can be modified in place.

35. How can you concatenate string values in Go? 

A. Using the '+' operator 
B. Using the 'append' function 
C. Using the 'concat' function 
D. Strings cannot be concatenated in Go 

Answer: 

A. Using the '+' operator 

Explanation:

To concatenate strings, we can use the addition operator (+). Note that each time we concatenate to a string value, Go creates a new string. That’s because strings are immutable in Go, making this inefficient.

Comments