Basic Go (Programming Language) MCQ
1. What is an idiomatic way to customize the representation of a custom struct in a formatted string?
There is no customizing the string representation of a type.
Build it in pieces each time by calling individual fields.
Implement a method String() string
Create a wrapper function that accepts your type and outputs a string.
Answer
Correct Answer:
Implement a method String() string
Note: This Question is unanswered, help us to find answer for this one
Check Answer
2. If you iterate over a map in a for range loop, in which order will the key:value pairs be accessed?
In pseudo-random order that cannot be predicted
In reverse order of how they were added, last in first out
Sorted by key in ascending order
In the order they were added, first in first out
Answer
Correct Answer:
In pseudo-random order that cannot be predicted
Note: This Question is unanswered, help us to find answer for this one
Check Answer
3. What is the correct syntax ta start a goroutine that will print Hello Gopher!?
go(fmt.Println("Hello Gopher!"))
go func() { fmt.Println("Hello Gopher!") }
go fmt.Println("Hello Gopher!")
Go fmt.Println("Hello Gopher!")
Answer
Correct Answer:
go fmt.Println("Hello Gopher!")
Note: This Question is unanswered, help us to find answer for this one
Check Answer
4. How can you compile main.go to an executable that will run on OSX arm64 ?
Set GOOS to arm64 and GOARCH to darwin.
Set GOOS to osx and GOARCH to arm64.
Set GOOS to arm64 and GOARCH to osx.
Set GOOS to darwin and GOARCH to 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
Check Answer
5. What is the common way to have several executables in your project?
Have a cmd directory and a directory per executable inside it.
Comment out main.
Use build tags.
Have a pkg directory and a directory per executable inside it.
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
Check Answer
6. Which file names will the go test command recognize as test files?
Any that starts with test
Any files that include the word test
Only files in the root directory that end in _test.go
Any that ends in _test.go
Answer
Correct Answer:
Any that ends in _test.go
Note: This Question is unanswered, help us to find answer for this one
Check Answer
7. Which is a valid Go time format literal?
"2006-01-02"
"YYYY-mm-dd"
"y-mo-d"
"year-month-day"
Answer
Correct Answer:
"2006-01-02"
Note: This Question is unanswered, help us to find answer for this one
Check Answer
8. What does log.Fatal do?
It raises a panic.
It prints the log and then raises a panic.
It prints the log and then safely exits the program.
It exits the program.
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
Check Answer
9. How is the behavior of t.Fatal different inside a t.Run?
There is no difference.
T.Fatal does not crash the test harness, preserving output messages.
T.Fatal stops execution of the subtest and continues with other test cases.
T.Fatal stops all tests and contains extra information about the failed subtest.
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
Check Answer
10. Which encodings can you put in a string?
Any, it accepts arbitary bytes
Any Unicode format
UTF-8 or ASCII
UTF-8
Note: This Question is unanswered, help us to find answer for this one
Check Answer
11. If your current working directory is the top level of your project, which command will run all its test packages?
Go test all
Go run --all
Go test .
Go test ./...
Answer
Correct Answer:
Go test ./...
Note: This Question is unanswered, help us to find answer for this one
Check Answer
12. How can you tell Go to import a package from a different location?
Use a proxy.
Change the import path.
Use a replace directive in go.mod.
Use a replace directory.
Answer
Correct Answer:
Use a replace directive in go.mod.
Note: This Question is unanswered, help us to find answer for this one
Check Answer
13. Which choice does not send output to standard error?
Println(message)
log.New(os.Stderr, "", 0).Println(message)
fmt.Errorf("%s\n", message)
Fmt.Fprintln(os.Stderr, message)
Answer
Correct Answer:
Println(message)
Note: This Question is unanswered, help us to find answer for this one
Check Answer
14. Where is the built-in recover method useful?
In the main function
Immediately after a line that might panic
Inside a deferred function
At the beginning of a function that might panic
Answer
Correct Answer:
Inside a deferred function
Note: This Question is unanswered, help us to find answer for this one
Check Answer
15. What is the risk of using multiple field tags in a single struct?
Every field must have all tags to compile.
It tightly couples different layers of your application.
Any tags after the first are ignored.
Missing tags panic at runtime.
Answer
Correct Answer:
It tightly couples different layers of your application.
Note: This Question is unanswered, help us to find answer for this one
Check Answer
16. What is the difference between the time package’s Time.Sub() and Time.Add() methods?
Time.Add() is for performing addition while Time.Sub() is for nesting timestamps.
Time.Add() always returns a later time while time.Sub always returns an earlier time.
They are opposites. Time.Add(x) is the equivalent of Time.Sub(-x).
Time.Add() accepts a Duration parameter and returns a Time while Time.Sub() accepts a Time parameter and returns a Duration.
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
Check Answer
17. What is the default case sensitivity of the JSON Unmarshal function?
The default behavior is case insensitive, but it can be overridden.
Fields are matched case sensitive.
Fields are matched case insensitive.
The default behavior is case sensitive, but it can be overridden.
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
Check Answer
18. A switch statement _ its own lexical block. Each case statement _ an additional lexical block.
Does not create; creates
Does not create; does not create
Creates; creates
Creates; does not create
Answer
Correct Answer:
Creates; creates
Note: This Question is unanswered, help us to find answer for this one
Check Answer
19. What should the idiomatic name be for an interface with a single method and the signature Save() error?
Saveable
SaveInterface
ISave
Saver
Note: This Question is unanswered, help us to find answer for this one
Check Answer
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"
resp, err := http.Post("https://httpbin.org/post", "text/plain", []byte(data))
resp, err := http.Post("https://httpbin.org/post", "text/plain", data)
resp, err := http.Post("https://httpbin.org/post", "text/plain", strings.NewReader(data))
resp, err := http.Post("https://httpbin.org/post", "text/plain", &data)
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
Check Answer
21. How can you make a file build only on Windows?
Check runtime.GOOS.
Add a // +build windows comment anywhere in the file.
Add a _ prefix to the file name.
Add a // +build windows comment at the top of the file.
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
Check Answer
22. What is a channel?
A global variable
A medium for sending values between goroutines
A dynamic array of values
A lightweight thread for concurrent programming
Answer
Correct Answer:
A medium for sending values between goroutines
Note: This Question is unanswered, help us to find answer for this one
Check Answer
23. What restriction is there on the type of var to compile this i := myVal.(int)?
MyVal must be an integer type, such as int, int64, int32, etc.
MyVal must be able to be asserted as an int.
MyVal must be an interface.
MyVal must be a numeric type, such as float64 or int64.
Answer
Correct Answer:
MyVal must be an interface.
Note: This Question is unanswered, help us to find answer for this one
Check Answer
24. What is the select statement used for?
Executing a function concurrently
Executing a different case based on the type of a variable
Executing a different case based on the value of a variable
Executing a different case based on which channel returns first
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
Check Answer
25. What is a side effect of using time.After in a select statement?
It blocks the other channels.
It is meant to be used in select statements without side effects.
It blocks the select statement until the time has passed.
The goroutine does not end until the time passes.
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
Check Answer
26. What is an idiomatic way to pause execution of the current scope until an arbitrary number of goroutines have returned?
Pass an int and Mutex to each and count when they return.
Loop over a select statement.
Sleep for a safe amount of time.
Sync.WaitGroup
Answer
Correct Answer:
Sync.WaitGroup
Note: This Question is unanswered, help us to find answer for this one
Check Answer
27. What does a sync.Mutex block while it is locked?
All goroutines
Any other call to lock that Mutex
Any reads or writes of the variable it is locking
Any writes to the variable it is locking
Answer
Correct Answer:
Any other call to lock that Mutex
Note: This Question is unanswered, help us to find answer for this one
Check Answer
28. How do you tell go test to print out the tests it is running?
Go test
Go test -x
Go test --verbose
Go test -v
Answer
Correct Answer:
Go test -v
Note: This Question is unanswered, help us to find answer for this one
Check Answer
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?
It can be accessed anywhere inside myPackage, not the rest of myModule.
It can be accessed by any application that imports myModule.
It can be accessed from anywhere in myModule.
It can be accessed by other packages in myModule as long as they import myPackage
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
Check Answer
30. Which is the only valid import statement in Go?
import "github/gin-gonic/gin"
import "https://github.com/gin-gonic/gin"
import "../template"
import "github.com/gin-gonic/gin"
Answer
Correct Answer:
import "github.com/gin-gonic/gin"
Note: This Question is unanswered, help us to find answer for this one
Check Answer
31. What is the value of Read?
Const (Write = iota; Read; Execute;)
1
2
A random value
Note: This Question is unanswered, help us to find answer for this one
Check Answer
32. How will you add the number 3 to the right side? Values := []int{1, 1, 2}
Values.append(3)
Values.insert(3, 3)
Append(values, 3)
Values = append(values, 3)
Answer
Correct Answer:
Values = append(values, 3)
Note: This Question is unanswered, help us to find answer for this one
Check Answer
33. Which is not a valid loop construct in Go?
Do { ... } while i < 5
For _,c := range
For i := 1; i < 5; i++ { ... }
For i < 5 { ... }
Answer
Correct Answer:
Do { ... } while i < 5
Note: This Question is unanswered, help us to find answer for this one
Check Answer
34. What does the len() function return if passed a UTF-8 encoded string?
The number of characters
The number of bytes
It does not accept string types.
The number of code points
Answer
Correct Answer:
The number of bytes
Note: This Question is unanswered, help us to find answer for this one
Check Answer
35. What do you need for two functions to be the same type?
They should share the same signatures, including parameter types and return types.
They should share the same parameter types but can return different types.
All functions should be the same type.
The functions should not be a first class 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
Check Answer
Go (Programming Language) MCQs | Topic-wise