Go (Programming Language) MCQs

Go (Programming Language) MCQs

Answer these 30+ Go (Programming Language) MCQs and see how sharp is your knowledge of Go (Programming Language).
Scroll down and let's start!

1: What do you need for two functions to be the same type?

A.   They should share the same signatures, including parameter types and return types.

B.   They should share the same parameter types but can return different types.

C.   All functions should be the same type.

D.   The functions should not be a first class type.

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

A.   The number of characters

B.   The number of bytes

C.   It does not accept string types.

D.   The number of code points

3: Which is not a valid loop construct in Go?

A.   Do { ... } while i < 5

B.   For _,c := range

C.   For i := 1; i < 5; i++ { ... }

D.   For i < 5 { ... }

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

A.   Values.append(3)

B.   Values.insert(3, 3)

C.   Append(values, 3)

D.   Values = append(values, 3)

5: What is the value of Read?

A.   Const (Write = iota; Read; Execute;)

B.   1

C.   2

D.   A random value

6: Which is the only valid import statement in Go?

A.   import "github/gin-gonic/gin"

B.   import "https://github.com/gin-gonic/gin"

C.   import "../template"

D.   import "github.com/gin-gonic/gin"

7: 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?

A.   It can be accessed anywhere inside myPackage, not the rest of myModule.

B.   It can be accessed by any application that imports myModule.

C.   It can be accessed from anywhere in myModule.

D.   It can be accessed by other packages in myModule as long as they import myPackage

8: How do you tell go test to print out the tests it is running?

A.   Go test

B.   Go test -x

C.   Go test --verbose

D.   Go test -v

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

A.   All goroutines

B.   Any other call to lock that Mutex

C.   Any reads or writes of the variable it is locking

D.   Any writes to the variable it is locking

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

A.   Pass an int and Mutex to each and count when they return.

B.   Loop over a select statement.

C.   Sleep for a safe amount of time.

D.   Sync.WaitGroup

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

A.   It blocks the other channels.

B.   It is meant to be used in select statements without side effects.

C.   It blocks the select statement until the time has passed.

D.   The goroutine does not end until the time passes.

12: What is the select statement used for?

A.   Executing a function concurrently

B.   Executing a different case based on the type of a variable

C.   Executing a different case based on the value of a variable

D.   Executing a different case based on which channel returns first

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

A.   MyVal must be an integer type, such as int, int64, int32, etc.

B.   MyVal must be able to be asserted as an int.

C.   MyVal must be an interface.

D.   MyVal must be a numeric type, such as float64 or int64.

14: What is a channel?

A.   A global variable

B.   A medium for sending values between goroutines

C.   A dynamic array of values

D.   A lightweight thread for concurrent programming

15: How can you make a file build only on Windows?

A.   Check runtime.GOOS.

B.   Add a // +build windows comment anywhere in the file.

C.   Add a _ prefix to the file name.

D.   Add a // +build windows comment at the top of the file.

16: 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"

A.   resp, err := http.Post("https://httpbin.org/post", "text/plain", []byte(data))

B.   resp, err := http.Post("https://httpbin.org/post", "text/plain", data)

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

D.   resp, err := http.Post("https://httpbin.org/post", "text/plain", &data)

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

A.   Saveable

B.   SaveInterface

C.   ISave

D.   Saver

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

A.   Does not create; creates

B.   Does not create; does not create

C.   Creates; creates

D.   Creates; does not create

19: What is the default case sensitivity of the JSON Unmarshal function?

A.   The default behavior is case insensitive, but it can be overridden.

B.   Fields are matched case sensitive.

C.   Fields are matched case insensitive.

D.   The default behavior is case sensitive, but it can be overridden.

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

A.   Time.Add() is for performing addition while Time.Sub() is for nesting timestamps.

B.   Time.Add() always returns a later time while time.Sub always returns an earlier time.

C.   They are opposites. Time.Add(x) is the equivalent of Time.Sub(-x).

D.   Time.Add() accepts a Duration parameter and returns a Time while Time.Sub() accepts a Time parameter and returns a Duration.

21: What is the risk of using multiple field tags in a single struct?

A.   Every field must have all tags to compile.

B.   It tightly couples different layers of your application.

C.   Any tags after the first are ignored.

D.   Missing tags panic at runtime.

22: Where is the built-in recover method useful?

A.   In the main function

B.   Immediately after a line that might panic

C.   Inside a deferred function

D.   At the beginning of a function that might panic

23: Which choice does not send output to standard error?

A.   Println(message)

B.   log.New(os.Stderr, "", 0).Println(message)

C.   fmt.Errorf("%s\n", message)

D.   Fmt.Fprintln(os.Stderr, message)

24: How can you tell Go to import a package from a different location?

A.   Use a proxy.

B.   Change the import path.

C.   Use a replace directive in go.mod.

D.   Use a replace directory.

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

A.   Go test all

B.   Go run --all

C.   Go test .

D.   Go test ./...

26: Which encodings can you put in a string?

A.   Any, it accepts arbitary bytes

B.   Any Unicode format

C.   UTF-8 or ASCII

D.   UTF-8

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

A.   There is no difference.

B.   T.Fatal does not crash the test harness, preserving output messages.

C.   T.Fatal stops execution of the subtest and continues with other test cases.

D.   T.Fatal stops all tests and contains extra information about the failed subtest.

28: What does log.Fatal do?

A.   It raises a panic.

B.   It prints the log and then raises a panic.

C.   It prints the log and then safely exits the program.

D.   It exits the program.

29: Which is a valid Go time format literal?

A.   "2006-01-02"

B.   "YYYY-mm-dd"

C.   "y-mo-d"

D.   "year-month-day"

30: How should you log an error (err)

A.   Log.Error(err)

B.   log.Printf("error: %v", err)

C.   log.Printf(log.ERROR, err)

D.   log.Print("error: %v", err)

31: Which file names will the go test command recognize as test files?

A.   Any that starts with test

B.   Any files that include the word test

C.   Only files in the root directory that end in _test.go

D.   Any that ends in _test.go

32: What is the common way to have several executables in your project?

A.   Have a cmd directory and a directory per executable inside it.

B.   Comment out main.

C.   Use build tags.

D.   Have a pkg directory and a directory per executable inside it.

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

A.   Set GOOS to arm64 and GOARCH to darwin.

B.   Set GOOS to osx and GOARCH to arm64.

C.   Set GOOS to arm64 and GOARCH to osx.

D.   Set GOOS to darwin and GOARCH to arm64.

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

A.   go(fmt.Println("Hello Gopher!"))

B.   go func() { fmt.Println("Hello Gopher!") }

C.   go fmt.Println("Hello Gopher!")

D.   Go fmt.Println("Hello Gopher!")

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

A.   In pseudo-random order that cannot be predicted

B.   In reverse order of how they were added, last in first out

C.   Sorted by key in ascending order

D.   In the order they were added, first in first out

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

A.   There is no customizing the string representation of a type.

B.   Build it in pieces each time by calling individual fields.

C.   Implement a method String() string

D.   Create a wrapper function that accepts your type and outputs a string.