linkedin-skill-assessments-quizzes

Go (Programming Language)

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

User defined function types in Go (Golang)

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

Length of string in Go (Golang).

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

      do { ... } while i < 5
      for _,c := range "hello" { ... }
      for i := 1; i < 5; i++ { ... }
      for i < 5 { ... }

Explanation: Go has only for-loops

Q4. 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)

Explanation: slices in GO are immutable, so calling append does not modify the slice

Q5. What is the value of Read?

const (
  Write = iota
  Read
  Execute
)

IOTA in Go (Golang)

Q6. 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"

Import in GoLang

Q7. What would happen if you attempted to compile and run this Go program?

package main
var GlobalFlag string
func main() {
  print("["+GlobalFlag+"]")
}
  1. variables in Go have initial values. For string type, it’s an empty string.
  2. Go Playground

Q8. 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?

Explanation: to make the variable available outside of myPackage change the name to MyVar. See also an example of Exported names in the Tour of Go.

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

test package

Q10. This code printed {0, 0}. How can you fix it?

type Point struct {
  x int
  y int
}

func main() {
  data := []byte(`{"x":1, "y": 2}`)
  var p Point
  if err := json.Unmarshal(data, &p); err != nil {
    fmt.Println("error: ", err)
  } else {
    fmt.Println(p)
  }
}
  1. How to Parse JSON in Golang?
  2. Go Playground

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

  1. Mutex in GoLang, sync.Mutex locks so only one goroutine at a time can access the locked variable.
  2. sync.Mutex

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

Explanation: this is exactly what sync.WaitGroup is designed for - Use sync.WaitGroup in Golang

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

Note: it doesn’t block select and does not block other channels.

  1. time.After() Function in Golang With Examples
  2. How can I use ‘time.After’ and ‘default’ in Golang?
  3. Go Playground example

Q14. What is the select statement used for?

Select statement in GoLang

Q15. According to the Go documentation standard, how should you document this function?

func Add(a, b int) int {
        return a + b
}

Explanation: documentation block should start with a function name

Comments in Go

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

Explanation: This kind of type casting (using .(type)) is used on interfaces only.

  1. this example
  2. Primitive types are type-casted differently
  3. Go Playground
  4. Type assertions

Q17. What is a channel?

Channels

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

  1. How to use conditional compilation with the go build tool, Oct 2013
  2. go commands Build constraints

//go:build windows “Go versions 1.16 and earlier used a different syntax for build constraints, with a “// +build” prefix. The gofmt command will add an equivalent //go:build constraint when encountering the older syntax.”

Q19. 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)
  1. net/http#Client.Post
  2. http.Post Golang example

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

Effective Go, Interface names

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

Go Language Core technology (Volume one) 1.5-scope

Relevant excerpt from the article:

The second if statement is nested inside the first, so a variable declared in the first if statement is visible to the second if statement. There are similar rules in switch: Each case has its own lexical block in addition to the conditional lexical block.

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

encoding/json#Unmarshal

Relevant excerpt from the article:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. By default, object keys which don’t have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).

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

  1. time#Time.Add
  2. time#Time.Sub

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

  1. Example Code / b29r0fUD1cp
  2. How To Use Struct Tags in Go

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

Example of Recover Function in Go (Golang)

Relevant excerpt from the article:

Recover is useful only when called inside deferred functions. Executing a call to recover inside a deferred function stops the panicking sequence by restoring normal execution and retrieves the error message passed to the panic function call. If recover is called outside the deferred function, it will not stop a panicking sequence.

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

  1. func println: writes the result to standard error.
  2. func New: func New(out io.Writer, prefix string, flag int) *Logger; the out variable sets the destination to which log data will be written.
  3. func Errorf: Errorf formats according to a format specifier and returns the string as a value.
  4. func Fprintln: func Fprintln(w io.Writer, a …any) (n int, err error); Fprintln formats using the default formats for its operands and writes to w.

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

  1. Call your code from another module: chapter 5., go mod edit -replace example.com/greetings=../greetings.
  2. go.mod replace directive

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

  1. Example of testing in Go (Golang)
  2. Example of cmd in Go (Golang)

Relevant excerpt from the article:

Relative patterns are also allowed, like “go test ./…” to test all subdirectories.

Q29. Which encodings can you put in a string?

  1. Strings, bytes, runes and characters in Go

Relevant excerpt from the article:

In short, Go source code is UTF-8, so the source code for the string literal is UTF-8 text.

  1. Example of encoding in Go (Golang)

Relevant excerpt from the article:

Package encoding defines an interface for character encodings, such as Shift JIS and Windows 1252, that can convert to and from UTF-8.

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

  1. Reference:
  2. testing package in Go, the relevant excerpt from the article:

Fatal is equivalent to Log followed by FailNow. Log formats its arguments using default formatting, analogous to Println, and records the text in the error log. FailNow marks the function as having failed and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines. Run runs f as a subtest of t called name. It runs f in a separate goroutine and blocks until f returns or calls t.Parallel to become a parallel test. Run reports whether f succeeded (or at least did not fail before calling t.Parallel). Run may be called simultaneously from multiple goroutines, but all such calls must return before the outer test function for t returns.

Q31. What does log.Fatal do?

Example of func Fatal in Go (Golang)

Relevant excerpt from the article:

Fatal is equivalent to Print() followed by a call to os.Exit(1).

Q32. Which is a valid Go time format literal?

func Time in Go

Relevant excerpt from the article:

Year: "2006" "06"
Month: "Jan" "January" "01" "1"
Day of the week: "Mon" "Monday"
Day of the month: "2" "_2" "02"
Day of the year: "__2" "002"
Hour: "15" "3" "03" (PM or AM)
Minute: "4" "04"
Second: "5" "05"
AM/PM mark: "PM"

Q33. How should you log an error (err)

Explanation: There is defined neither log.ERROR, nor log.Error() in log package in Go; log.Print() arguments are handled in the manner of fmt.Print(); log.Printf() arguments are handled in the manner of fmt.Printf().

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

  1. Test packages in go command in Go: ‘Go test’ recompiles each package along with any files with names matching the file pattern “*_test.go”.
  2. Add a test in Go

Q35. What will be the output of this code?

ch := make(chan int)
ch <- 7
val := <-ch
fmt.Println(val)

Go Playground share, output:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
 /tmp/sandbox2282523250/prog.go:7 +0x37

Program exited.

Q36. What will be the output of this program?

ch := make(chan int)
close(ch)
val := <-ch
fmt.Println(val)
  1. The Go Programming Language Specification “Receive operator”, Relevant excerpt from the article:

    A receive operation on a closed channel can always proceed immediately, yielding the element type’s zero value after any previously sent values have been received.

  2. Go Playground share, output:

0

Program exited.

Q37. What will be printed in this code?

var stocks map[string]float64 // stock -> price
price := stocks["MSFT"]
fmt.Printf("%f\n", price)

Go Playground share, output:

0.000000

Program exited.

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

  1. stackoverflow
  2. medium
  3. medium

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

documentation

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

Example of start a goroutine

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

Reference

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

Reference

Q43. How can you avoid a goroutine leak in this code?

func findUser(ctx context.Context, login string) (*User, error) {
    ch := make(chan *User)
    go func() {
            ch <- findUserInDB(login)
    }()

    select {
    case user := <-ch:
            return user, nil
    case <-ctx.Done():
            return nil, fmt.Errorf("timeout")
    }
}

Reference

Relevant excerpt from the article:

The simplest way to resolve this leak is to change the channel from an unbuffered channel to a buffered channel with a capacity of 1. Now in the timeout case, after the receiver has moved on, the Goroutine will complete its send by placing the *User value in the channel then it will return.

44. What will this code print?

var i int8 = 120
i += 10
fmt.Println(i)

Go Playground example, output:

-126

Program exited.

45. Given the definition of worker below, what is the right syntax to start a goroutine that will call worker and send the result to a channel named ch?

func worker(m Message) Result
go func() {
    r := worker(m)
    ch <- r
}
go func() {
    r := worker(m)
    r -> ch
} ()
go func() {
    r := worker(m)
    ch <- r
} ()
go ch <- worker(m)

Go Playground example

Q46. In this code, which names are exported?

package os

type FilePermission int
type userID int

Reference 1 Reference 2

Q47. Which of the following is correct about structures in Go?

Q48. What does the built-in generate command of the Go compiler do?

Generate Go files by processing source

Q49. Using the time package, how can you get the time 90 minutes from now?

func (Time) Add example

Q50. A program uses a channel to print five integers inside a goroutine while feeding the channel with integers from the main routine, but it doesn’t work as is. What do you need to change to make it work?

Reference

Relevant excerpt from the article:

The simplest way to resolve this leak is to change the channel from an unbuffered channel to a buffered channel with a capacity of 1. Now in the timeout case, after the receiver has moved on, the Goroutine will complete its send by placing the *User value in the channel then it will return.

Q51. After importing encoding/json, how will you access the Marshal function?

encoding/json#Marshal example

Q52. What are the two missing segments of code that would complete the use of context.Context to implement a three-second timeout for this HTTP client making a GET request?

package main

import (
        "context"
        "fmt"
        "net/http"
)

func main() {
        var cancel context.CancelFunc
        ctx := context.Background()

        // #1: <=== What should go here?

        req, _ := http.NewRequest(http.MethodGet,
                "https://linkedin.com",
                nil)

        // #2: <=== What should go here?

        client := &http.Client{}
        res, err := client.Do(req)
        if err != nil {
                fmt.Println("Request failed:", err)
                return
        }
        fmt.Println("Response received, status code:",
                res.StatusCode)
}
      ctx.SetTimeout(3*time.Second)
      req.AttachContext(ctx)
      ctx, cancel = context.WithTimeout(ctx, 3*time.Second); defer cancel()
      req = req.WithContext(ctx)
      ctx, cancel = context.WithTimeout(ctx, 3*time.Second); defer cancel() #2: req.AttachContext(ctx)
      ctx.SetTimeout(3*time.Second)
      req = req.WithContext(ctx)
  1. context#WithTimeout
  2. net/http#Request.WithContext

Q53. If you have a struct named Client defined in the same .go file as the statement, how do you export a variable with a default value so the variable is accessible by other packages?

Q54. This program outputs {Master Chief Spartan Protagonist Halo}. How would you get it to output Master Chief - a Spartan - is the Protagonist of Halo instead?

package main

import "fmt"

type Character struct{
        Name  string
        Class string
        Role  string
        Game  string
}

func main() {
        mc := Character{
                Name: "Master Chief",
                Class: "Spartan",
                Role: "Protagonist",
                Game: "Halo",
        }
        fmt.Println(mc)
}
  1. fmt#Stringer

Q55. How would you implement a working Append() method for Clients?

package main

type Client struct {
  Name string
}
type Clients struct {
  clients []*Client
}
func main() {
  c:= &Clients{clients.make([]*Client,0)}
  c.Append(&Client{Name: "LinkedIn API})
}

Q56. How would you recover from a panic() thrown by a called function without allowing your program to fail assuming your answer will run in the same scope where your function call will experience the panic?

Q57. What will this code print?

var n int
fmt.Println (n)

This is because in Go, when a variable is declared but not explicitly initialized, it is assigned a default zero value based on its type. For integers like n, the zero value is 0.

Q58. When creating a formatted string, which verb should you use to call the String () string method of a custom type?

In Go, the %s verb is used to format a string. When used with a custom type that has a String() method defined, the String() method will be automatically called and its return value will be used in the formatted string.

Q59. Which is not a valid value for layout when calling time. Now ( ) . Format ( layout)?

time#Time.Format

According to the documentation, the value 1 and 01 will represent the current month.

each layout string is a representation of the time stamp,
	Jan 2 15:04:05 2006 MST
An easy way to remember this value is that it holds, when presented in this order, the values (lined up with the elements above):
	  1 2  3  4  5    6  -7

Q60. How would you signal to the Go compiler that the Namespace struct must implement the JSONConverter interface? This question assumes the answer would be included in the same package where Namespace is declared.

This syntax creates a variable _ with the type of JSONConverter and assigns to it a value of (*Namespace)(nil). This essentially checks that the Namespace struct satisfies the JSONConverter interface by ensuring that it can be assigned to a variable of type JSONConverter.

Q61. Which statement about typing and interfaces is false?

  1. an interface.

In Go, a type automatically satisfies an interface if it implements all the methods of that interface. There is no need to explicitly declare that a struct implements an interface using a specific keyword.

Q62. How would you complete this program to generate the specified output, assuming the SQL table

===[Output]================
1: &{GameId:1 Title:Wolfenstein YearReleased:1992}
2: &{GameId:2 Title:Doom YearReleased:1993}
3: &{GameId:3 Title:Quake YearReleased:1996}

===[main.go]================
package main

import (
        "database/sql"
        "fmt"
        _ "github.com/go-sql-driver/mysql"
        "log"
)

type Game struct {
        GameId       int
        Title        string
        YearReleased int
}

func main() {

        conn, err := sql.Open("mysql",
                "john_carmack:agiftw!@tcp(localhost:3306)/idsoftware")

        if err != nil {
                panic(err)
        }
        defer func() { _ = conn.Close() }()

        results, err := conn.Query("SELECT game_id,title,year_released FROM games;")
        if err != nil {
                panic(err)
        }
        defer func() { _ = results.Close() }()

        // #1 <=== What goes here?

        for results.Next() {
                var g Game

                // #2 <=== What goes here?

                if err != nil {
                        panic(err)
                }

                // #3 <=== What goes here?

        }

        for i, g := range games {
                fmt.Printf("%d: %+v\n", i, g)
        }

}
#1: games := make([]*Game, results.RowsAffected())
#2: g, err = results.Fetch()
#3: games[results.Index()] = &g
#1: games := []Game{}
#2: g, err = results.Fetch()
#3: games = append(games,g)
#1: games := map[int]Game{}
#2: err = results.Scan(&g)
#3: games[g.GameId] = g
#1: games := make(map[int]*Game, 0)
#2: err = results.Scan(&g.GameId, &g.Title, &g.YearReleased)
#3: games[g.GameId] = &g

Q63. Fill in the blanks

  1. Test files in Go must _.
  2. Individual tests are identified by _.
  3. You can run subtests by __.
  4. You log the error and mark the test failed by _.

Q64. Which type is a rune an alias for?

  1. Strings, bytes, runes and characters in Go

Relevant excerpt from the article:

The Go language defines the word rune as an alias for the type int32, so programs can be clear when an integer value represents a code point.

Q65. When can you use the := syntax to assign to multiple variables? For example:

x, err := myFunc()
  1. Short variable declarations

Q66. How can You view the profiler output in cpu.pprof in the browser?

Q67. When does a variable of type interface{} evaluate to nil?

Q68. What value does a string variable hold if it has been allocated but not assigned?

If a string variable has been allocated but not assigned a value, its default value is an empty string “”. In Go, uninitialized string variables are automatically assigned the zero value for their respective type, which for strings is an empty string.

Q69. Which built-in function is used to stop a program from continuing?

The built-in function used to stop a program from continuing is panic(). When panic() is called, it triggers a panic, which stops the normal execution flow of the program and begins panicking. If the panic is not recovered, the program terminates.

Q70. What will the output be?

a,b := 1, 2
b,c:= 3, 4
fmt.Println(a, b, c)

Go Playground Example

Q71. What is the operator for a logical AND condition?

Q72. What is an anonymous function in Go?

Q73.Which keyword is used to declare an anonymous function in Go?

Q74. What is the main advantage of using anonymous functions in Go?

Explanation: they can be defined inline where they are used, offering more flexibility in code organization.

Q75. What is the syntax for calling an anonymous function immediately after its declaration in Go?

reference

Q76. Which types can Go developers define methods for?

Methods can be defined for any named type that is not a built-in type. When you create a new type using a type declaration, it becomes a named type, and you can define methods specific to that type. However, methods cannot be directly attached to built-in types like int, string, etc. reference