MCQs > IT & Programming > Go (Programming Language) MCQs > Basic Go (Programming Language) MCQs

Basic Go (Programming Language) MCQ

1. What is an idiomatic way to customize the representation of a custom struct in a formatted string?

Answer

Correct Answer: Implement a method String() string

Note: This Question is unanswered, help us to find answer for this one

2. If you iterate over a map in a for range loop, in which order will the key:value pairs be accessed?

Answer

Correct Answer: In pseudo-random order that cannot be predicted

Note: This Question is unanswered, help us to find answer for this one

3. What is the correct syntax ta start a goroutine that will print Hello Gopher!?

Answer

Correct Answer: go fmt.Println("Hello Gopher!")

Note: This Question is unanswered, help us to find answer for this one

4. How can you compile main.go to an executable that will run on OSX arm64 ?

Answer

Correct Answer: Set GOOS to darwin and GOARCH to arm64.

Note: This Question is unanswered, help us to find answer for this one

5. What is the common way to have several executables in your project?

Answer

Correct Answer: Have a cmd directory and a directory per executable inside it.

Note: This Question is unanswered, help us to find answer for this one

6. Which file names will the go test command recognize as test files?

Answer

Correct Answer: Any that ends in _test.go

Note: This Question is unanswered, help us to find answer for this one

7. Which is a valid Go time format literal?

Answer

Correct Answer: "2006-01-02"

Note: This Question is unanswered, help us to find answer for this one

8. What does log.Fatal do?

Answer

Correct Answer: It prints the log and then safely exits the program.

Note: This Question is unanswered, help us to find answer for this one

9. How is the behavior of t.Fatal different inside a t.Run?

Answer

Correct Answer: T.Fatal stops execution of the subtest and continues with other test cases.

Note: This Question is unanswered, help us to find answer for this one

10. Which encodings can you put in a string?

Answer

Correct Answer: UTF-8

Note: This Question is unanswered, help us to find answer for this one

11. If your current working directory is the top level of your project, which command will run all its test packages?

Answer

Correct Answer: Go test ./...

Note: This Question is unanswered, help us to find answer for this one

12. How can you tell Go to import a package from a different location?

Answer

Correct Answer: Use a replace directive in go.mod.

Note: This Question is unanswered, help us to find answer for this one

13. Which choice does not send output to standard error?

Answer

Correct Answer: Println(message)

Note: This Question is unanswered, help us to find answer for this one

14. Where is the built-in recover method useful?

Answer

Correct Answer: Inside a deferred function

Note: This Question is unanswered, help us to find answer for this one

15. What is the risk of using multiple field tags in a single struct?

Answer

Correct Answer: It tightly couples different layers of your application.

Note: This Question is unanswered, help us to find answer for this one

16. What is the difference between the time package’s Time.Sub() and Time.Add() methods?

Answer

Correct Answer: Time.Add() accepts a Duration parameter and returns a Time while Time.Sub() accepts a Time parameter and returns a Duration.

Note: This Question is unanswered, help us to find answer for this one

17. What is the default case sensitivity of the JSON Unmarshal function?

Answer

Correct Answer: The default behavior is case insensitive, but it can be overridden.

Note: This Question is unanswered, help us to find answer for this one

18. A switch statement _ its own lexical block. Each case statement _ an additional lexical block.

Answer

Correct Answer: Creates; creates

Note: This Question is unanswered, help us to find answer for this one

19. What should the idiomatic name be for an interface with a single method and the signature Save() error?

Answer

Correct Answer: Saver

Note: This Question is unanswered, help us to find answer for this one

20. What is the correct way to pass this as a body of an HTTP POST request? data := "A group of Owls is called a parliament"

Answer

Correct Answer: resp, err := http.Post("https://httpbin.org/post", "text/plain", strings.NewReader(data))

Note: This Question is unanswered, help us to find answer for this one

21. How can you make a file build only on Windows?

Answer

Correct Answer: Add a // +build windows comment at the top of the file.

Note: This Question is unanswered, help us to find answer for this one

22. What is a channel?

Answer

Correct Answer: A medium for sending values between goroutines

Note: This Question is unanswered, help us to find answer for this one

23. What restriction is there on the type of var to compile this i := myVal.(int)?

Answer

Correct Answer: MyVal must be an interface.

Note: This Question is unanswered, help us to find answer for this one

24. What is the select statement used for?

Answer

Correct Answer: Executing a different case based on which channel returns first

Note: This Question is unanswered, help us to find answer for this one

25. What is a side effect of using time.After in a select statement?

Answer

Correct Answer: It is meant to be used in select statements without side effects.

Note: This Question is unanswered, help us to find answer for this one

26. What is an idiomatic way to pause execution of the current scope until an arbitrary number of goroutines have returned?

Answer

Correct Answer: Sync.WaitGroup

Note: This Question is unanswered, help us to find answer for this one

27. What does a sync.Mutex block while it is locked?

Answer

Correct Answer: Any other call to lock that Mutex

Note: This Question is unanswered, help us to find answer for this one

28. How do you tell go test to print out the tests it is running?

Answer

Correct Answer: Go test -v

Note: This Question is unanswered, help us to find answer for this one

29. From where is the variable myVar accessible if it is declared outside of any functions in a file in package myPackage located inside module myModule?

Answer

Correct Answer: It can be accessed anywhere inside myPackage, not the rest of myModule.

Note: This Question is unanswered, help us to find answer for this one

30. Which is the only valid import statement in Go?

Answer

Correct Answer: import "github.com/gin-gonic/gin"

Note: This Question is unanswered, help us to find answer for this one

31. What is the value of Read?

Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

32. How will you add the number 3 to the right side? Values := []int{1, 1, 2}

Answer

Correct Answer: Values = append(values, 3)

Note: This Question is unanswered, help us to find answer for this one

33. Which is not a valid loop construct in Go?

Answer

Correct Answer: Do { ... } while i < 5

Note: This Question is unanswered, help us to find answer for this one

34. What does the len() function return if passed a UTF-8 encoded string?

Answer

Correct Answer: The number of bytes

Note: This Question is unanswered, help us to find answer for this one

35. What do you need for two functions to be the same type?

Answer

Correct Answer: They should share the same signatures, including parameter types and return types.

Note: This Question is unanswered, help us to find answer for this one