Beginner Go test (Beginner)
25 questions · 30 minutes · Free
Syntax, slices, maps, structs and error handling.
What it covers
- Types, slices and maps
- Structs and methods
- Error handling
- Packages and modules
- Loops and control flow
Interview format
- Questions: 25 questions — 15 multiple choice, 8 multi-answer, 2 predict-the-output
- Time: 30 minutes, timed from the first question
- Difficulty: Beginner
- Skill area: Backend
- Price: Free
Earn a certificate
The Beginner Go test is one of the 6 tests of the Go Backend Developer: Beginner learning path. Pass it and the other 5 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Beginner Go test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Which of the following is the correct syntax for a struct literal in Go?
- struct { Name string; Age int }{ Name: "Alice", Age: 30 }
- struct { Name string, Age int }{ Name: "Alice", Age: 30 }
- struct { Name string; Age int }(Name: "Alice", Age: 30)
- struct { Name string, Age int }(Name: "Alice", Age: 30)
Answer: A. struct { Name string; Age int }{ Name: "Alice", Age: 30 }
The correct syntax for a struct literal in Go uses curly braces and field names with colons to assign values. Option 0 follows this syntax correctly. Other options use incorrect syntax such as parentheses or missing field names.
Consider the following Go code snippet: `s := []int{1, 2}; s = append(s, 3); fmt.Println(len(s), s)`. What is the output of this code?
- The length of the slice is 4, and the slice contains [1, 2, 3, 0].
- The length of the slice is 3, and the slice contains [3, 2, 1].
- The length of the slice is 2, and the slice contains [1, 2].
- The length of the slice is 3, and the slice contains [1, 2, 3].
Answer: D. The length of the slice is 3, and the slice contains [1, 2, 3].
When using the `append` function on a slice, if the capacity is sufficient, the element is added to the existing slice. In the given example, the slice `s` is initially `[1, 2]` with a capacity of 2. After appending `3`, the slice's length increases to 3, and the slice becomes `[1, 2, 3]`. The `append` function returns the new slice, which is correctly assigned to `s`.
Which of the following is the correct way to unwrap a wrapped error in Go?
- Using errors.Is() to check for a specific error type
- Using errors.New() to create a new error
- Using errors.As() to assign an error to a variable
- Using errors.Unwrap() to retrieve the underlying error
Answer: D. Using errors.Unwrap() to retrieve the underlying error
In Go, when dealing with wrapped errors, the errors.Unwrap() function is used to retrieve the underlying error. This allows for inspecting the original error that was wrapped. Other options are incorrect because they either refer to non-existent functions or do not correctly address the mechanism for unwrapping errors.
Which of the following best describes the difference between maps and structs in Go?
- Maps are used for numerical data only, while structs are used for textual data.
- Maps are used for grouping related data with fixed fields, while structs are used for dynamic key-value associations.
- Maps are used when the keys are not known in advance and can vary dynamically, while structs are used when the data is fixed and related.
- Maps and structs are interchangeable and can be used in all scenarios.
Answer: C. Maps are used when the keys are not known in advance and can vary dynamically, while structs are used when the data is fixed and related.
Maps are ideal for scenarios where you need to associate values with keys that can change or are not known ahead of time. Structs, on the other hand, are used to group related data with fixed fields. This distinction helps in choosing the right data structure based on the use case.
Which statement correctly describes how to control the visibility of struct fields in Go?
- Use lowercase letters for all struct fields, as uppercase letters are reserved for method names only.
- Use uppercase letters for struct fields that need to be accessible outside the package, and lowercase letters for fields that should remain private within the package.
- Use a mix of uppercase and lowercase letters randomly, as Go automatically determines field visibility based on the field's position in the struct definition.
- Use uppercase letters for all struct fields, as lowercase letters are not allowed in Go struct definitions.
Answer: B. Use uppercase letters for struct fields that need to be accessible outside the package, and lowercase letters for fields that should remain private within the package.
In Go, struct field visibility is determined by the case of the first letter of the field name. Uppercase letters make the field exported (accessible outside the package), while lowercase letters make it unexported (private to the package). This rule applies to all identifiers in Go, including struct fields, methods, and variables.
Frequently asked questions
- How many questions are in the Beginner Go test?
- 25 questions (15 multiple choice, 8 multi-answer, 2 predict-the-output), with 30 minutes to answer them.
- What does the Beginner Go test cover?
- Types, slices and maps, Structs and methods, Error handling, Packages and modules, Loops and control flow.
- Is the Beginner Go test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Beginner Go test?
- Beginner, in Backend.