Go Programming Skills Assessment — Questions and Answers
Question 1: What naming convention determines if a Go identifier is exported from a package?
- Start the name with an uppercase letter (Correct answer)
- Prefix with `export`
- Prefix with `pub`
- Add `//export` comment above it
Correct answer: Start the name with an uppercase letter
In Go, identifiers starting with an uppercase letter are exported (public), while lowercase identifiers are unexported (package-private).
Question 2: What is a nil channel's behavior when read from or written to?
- Causes compile error
- Panics immediately
- Returns zero value
- Blocks forever (Correct answer)
Correct answer: Blocks forever
Both sends and receives on a nil channel block forever, which can be used intentionally to disable a select case.
Question 3: What does the `select` statement do when multiple channels are ready simultaneously?
- Executes the first case in order
- Picks a ready case pseudo-randomly (Correct answer)
- Panics with ambiguous select
- Executes all ready cases
Correct answer: Picks a ready case pseudo-randomly
When multiple cases in a `select` are ready, Go picks one uniformly at random to prevent starvation and avoid deterministic bias.
Question 4: Which Go construct allows you to create an anonymous struct inline?
- inline struct(Field Type)
- anonymous{ Field Type }
- struct{ Field Type }{value} (Correct answer)
- var s = struct(Field string)
Correct answer: struct{ Field Type }{value}
Anonymous structs are created inline using `struct{ Field Type }{value}` syntax without giving the struct a named type.
Question 5: What does the `replace` directive in `go.mod` allow you to do?
- Substitute a module dependency with a local path or different version (Correct answer)
- Replace the Go compiler version
- Replace a package name globally
- Override init() functions
Correct answer: Substitute a module dependency with a local path or different version
The `replace` directive maps a module path to a different path or version, commonly used for local development or forking a dependency.
Question 6: How does Go handle method sets for a value type `T` versus a pointer type `*T`?
- Pointer type *T can only call pointer receiver methods
- Value type T can call pointer receiver methods but not vice versa
- Both T and *T have identical method sets
- Value type T can only call value receiver methods; pointer type *T can call both value and pointer receiver methods (Correct answer)
Correct answer: Value type T can only call value receiver methods; pointer type *T can call both value and pointer receiver methods
The method set of `T` includes only value receiver methods, while the method set of `*T` includes both value and pointer receiver methods.
Question 7: Why is `chan struct{}` preferred over `chan bool` for signaling in Go?
- struct{} consumes zero bytes, minimizing memory overhead (Correct answer)
- struct{} has faster sends
- struct{} channels are unbuffered by default
- bool channels do not support close
Correct answer: struct{} consumes zero bytes, minimizing memory overhead
`struct{}` is an empty type with zero size, so a `chan struct{}` uses no memory for the values themselves, making it the most efficient signal channel.
Question 8: What command displays the dependency graph of the current module?
- go mod graph (Correct answer)
- go deps
- go mod list
- go list -deps
Correct answer: go mod graph
`go mod graph` prints the module dependency graph as a list of pairs, showing which module requires which other module.
Question 9: What is the `internal` package convention in Go?
- Internal packages skip type checking
- Internal packages are hidden from the module system
- Code in an `internal` directory can only be imported by code in its parent tree (Correct answer)
- Packages named `internal` are compiled differently
Correct answer: Code in an `internal` directory can only be imported by code in its parent tree
The `internal` directory restriction prevents external packages from importing its contents; only packages rooted at the parent of `internal` may import it.
Question 10: What is the primary purpose of the `sync.WaitGroup` in Go?
- Cancel goroutine execution
- Share data between goroutines
- Wait for a collection of goroutines to finish (Correct answer)
- Limit goroutine count
Correct answer: Wait for a collection of goroutines to finish
`sync.WaitGroup` blocks the calling goroutine until all goroutines tracked via `Add`, `Done`, and `Wait` have completed.
Question 11: What is the purpose of using `new(T)` versus `&T{}` when creating a struct in Go?
- `new(T)` can only be used with primitive types, not structs
- `new(T)` returns a value; `&T{}` returns a pointer
- `new(T)` allocates on the stack; `&T{}` always allocates on the heap
- Both allocate a zeroed struct and return a pointer; `&T{}` also lets you set initial field values (Correct answer)
Correct answer: Both allocate a zeroed struct and return a pointer; `&T{}` also lets you set initial field values
`new(T)` and `&T{}` both allocate a zeroed struct and return a pointer, but `&T{}` additionally allows specifying initial field values in the literal.
Question 12: How do you enable fuzz testing in Go (introduced in Go 1.18)?
- go test -fuzz=FuzzFoo (Correct answer)
- go test -mutate FuzzFoo
- go fuzz FuzzFoo
- go test -random FuzzFoo
Correct answer: go test -fuzz=FuzzFoo
`go test -fuzz=FuzzFoo` runs the fuzz target named `FuzzFoo`, which must follow `FuzzXxx(f *testing.F)` naming and use `f.Fuzz` to define the fuzz function.
Question 13: Which statement about Go's package declaration is true?
- A Go program can have multiple package main declarations.
- Every Go file must start with a package declaration. (Correct answer)
- The package declaration can be omitted for small programs.
- The package main is required to create an executable program. (Correct answer)
Correct answer: Every Go file must start with a package declaration.
In Go, every source file must begin with a `package` declaration, defining which package the file belongs to. For a program to be executable, it must contain a `main` package, which includes a `main` function as its entry point. While a Go program can consist of multiple files, only one `main` package can exist per executable, serving as the program's starting point.
Question 14: What is a pipeline pattern in Go?
- A single goroutine processing a list
- A series of stages connected by channels, each processing and passing data (Correct answer)
- Parallel sorting of a slice
- Chaining functions without channels
Correct answer: A series of stages connected by channels, each processing and passing data
A Go pipeline is a series of stages where each stage receives values from upstream via a channel, processes them, and sends results to the next stage via another channel.
Question 15: What is the zero value of memory in Go when allocated with new?
- nil for pointers. (Correct answer)
- "" for strings. (Correct answer)
- Random garbage values for structs.
- 0 for numeric types. (Correct answer)
Correct answer: nil for pointers.
When memory is allocated with `new` in Go, the allocated value is always zero-initialized. This means numeric types like integers and floats are initialized to `0`, booleans to `false`, strings to `""` (empty string), and pointers to `nil`. Go avoids 'random garbage values' to ensure predictable behavior and prevent common programming errors.
Question 16: Which of the following strings package functions are used to manipulate strings in Go?
- strings.ToUpper (Correct answer)
- strings.Replace (Correct answer)
- strings.Contains (Correct answer)
- strings.Sort
Correct answer: strings.ToUpper
The `strings` package in Go provides a rich set of functions for string manipulation. `strings.Contains` checks if a substring is present, `strings.ToUpper` converts a string to uppercase, and `strings.Replace` replaces occurrences of a substring. `strings.Sort` is not a function in the `strings` package; sorting is typically done using the `sort` package on slices of strings.
Question 17: Which package provides the `Mutex` type for protecting shared state in Go?
- atomic
- runtime
- os
- sync (Correct answer)
Correct answer: sync
The `sync` package provides `Mutex` and `RWMutex` for safe concurrent access to shared data.
Question 18: What is the fan-out concurrency pattern in Go?
- Goroutines share a single mutex
- One goroutine reads from multiple channels
- Multiple channels merge into one
- One input channel is read by multiple goroutines in parallel (Correct answer)
Correct answer: One input channel is read by multiple goroutines in parallel
Fan-out distributes work by having multiple goroutines read from a single input channel, processing jobs in parallel.
Question 19: What happens when you send to a full buffered channel with no receiver?
- The channel capacity doubles
- The send is discarded
- The goroutine blocks until space is available (Correct answer)
- The send panics
Correct answer: The goroutine blocks until space is available
Sending to a full buffered channel blocks the sending goroutine until another goroutine receives and frees space in the buffer.
Question 20: What is the significance of `b.N` in a Go benchmark?
- The target benchmark duration in nanoseconds
- The number of goroutines to use
- The number of iterations the benchmark function should execute its inner loop (Correct answer)
- The number of CPUs to use
Correct answer: The number of iterations the benchmark function should execute its inner loop
`b.N` is automatically adjusted by the benchmark framework until the benchmark runs long enough to produce a stable measurement; your loop must iterate `b.N` times.
Question 21: What keyword is used to start a goroutine in Go?
- spawn
- async
- goroutine
- go (Correct answer)
Correct answer: go
The `go` keyword before a function call launches it as a goroutine, running concurrently with other goroutines.
Question 22: How does Go determine if a type implements an interface?
- The type must declare it with `implements`
- The type must have all methods in the interface with matching signatures (Correct answer)
- The type must embed the interface
- The type must be registered with the compiler
Correct answer: The type must have all methods in the interface with matching signatures
Go uses implicit interface satisfaction: a type implements an interface if it defines all the interface's methods with identical signatures, requiring no explicit declaration.
Question 23: What happens if you send on a closed channel?
- The value is silently dropped
- A compile error occurs
- A panic occurs at runtime (Correct answer)
- The send blocks indefinitely
Correct answer: A panic occurs at runtime
Sending to a closed channel causes a runtime panic: `send on closed channel`.
Question 24: What happens if a Go source file imports a package but does not use it?
- A warning is emitted
- A compile error occurs (Correct answer)
- The unused import is ignored
- The program compiles with reduced performance
Correct answer: A compile error occurs
Go enforces that every imported package must be used; an unused import causes a compile-time error, keeping codebases clean.
Question 25: How do you detect race conditions when running Go tests?
- go test -concurrent
- go test -race (Correct answer)
- go test -verbose
- go test -sync
Correct answer: go test -race
The `-race` flag enables Go's built-in race detector, which instruments the code to report data races at runtime.
Question 26: What is a race condition in Go?
- Two goroutines accessing shared memory without proper synchronization (Correct answer)
- Deadlock caused by channel blocking
- Two goroutines finishing at the same time
- A goroutine running faster than expected
Correct answer: Two goroutines accessing shared memory without proper synchronization
A race condition occurs when two or more goroutines access shared memory concurrently and at least one access is a write, without synchronization.
Question 27: What keyword is used to embed a type anonymously in a struct?
- Just the type name without a field name (Correct answer)
- include
- Anonymous
- embed
Correct answer: Just the type name without a field name
Anonymous (embedded) fields are declared with just the type name (and no explicit field name), e.g., `type S struct { T }` embeds `T` in `S`.
Question 28: Which function returns the number of logical CPUs available to the current process?
- os.NumCPU()
- runtime.NumCPU() (Correct answer)
- runtime.GOMAXPROCS(0)
- sync.NumCPU()
Correct answer: runtime.NumCPU()
`runtime.NumCPU()` returns the number of logical CPUs on the machine, while `GOMAXPROCS` sets/gets the limit used by the scheduler.
Question 29: What does garbage collection do in Go?
- Requires explicit invocation by the programmer.
- Manually frees allocated memory.
- Automatically reclaims memory that is no longer in use. (Correct answer)
- Prevents memory leaks by stopping cyclic references. (Correct answer)
Correct answer: Automatically reclaims memory that is no longer in use.
Go features an automatic garbage collector that manages memory allocation and deallocation. Its primary role is to automatically identify and reclaim memory that is no longer referenced by the program, preventing memory leaks and reducing the burden on developers for manual memory management. This process ensures efficient use of system resources and effectively handles cyclic references, which can otherwise lead to memory leaks.
Question 30: How do you define a method on a struct type `Point` in Go?
- Point.func Method() {}
- func (p Point) Method() {} (Correct answer)
- func Method(Point p) {}
- func Point.Method() {}
Correct answer: func (p Point) Method() {}
In Go, methods are defined with a receiver between the `func` keyword and the method name, written as `func (p Point) Method() {}`.
Question 31: What happens when a goroutine panics and the panic is not recovered?
- The panic is silently ignored
- Other goroutines are notified
- The entire program crashes (Correct answer)
- Only that goroutine exits
Correct answer: The entire program crashes
An unrecovered panic in any goroutine terminates the entire Go program, not just that goroutine.
Go Programming Skills Assessment
This Go programming skills assessment evaluates a developer's proficiency in the Go language, covering core syntax, concurrency patterns, interfaces, error handling, standard library usage, and testing. Modeled after industry skills tests such as HackerRank's Go Basic and Intermediate certifications, this timed assessment is used to validate hands-on Go development competency for professional roles.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds