Go (Golang) Quiz: 50 Questions

Q1. What file extension is used for Go source files?
Go source files use a short extension.
Answer: .go — Go source files conventionally use the .go extension.
Q2. Which command builds and installs packages and dependencies?
Installs binaries to GOPATH/bin or module bin.
Answer: go install — compiles and installs packages and commands.
Q3. Which package is required for formatted I/O like Println?
Commonly used for printing to stdout.
Answer: fmt — fmt.Println, fmt.Printf are in the fmt package.
Q4. Fill: The entry point of a Go program is package
Executable programs use a special package name.
Answer: main — package main with func main() is the program entry point.
Q5. Which function signature is the program entry point?
Exact lowercase name required.
Answer: func main() — the runtime calls main in package main to start the program.
Q6. Which keyword declares a variable with inferred type?
Short variable declaration operator.
Answer: := — e.g., x := 5 declares x with inferred type int.
Q7. Which type represents a sequence of bytes and is immutable?
Used for text data.
Answer: string — immutable sequence of bytes representing text in Go.
Q8. Fill: A rune in Go is an alias for type
Represents a Unicode code point.
Answer: int32 — rune is an alias for int32 in Go.
Q9. Which composite type is an ordered collection with dynamic length?
Built on top of arrays with length and capacity.
Answer: slice — slices are dynamic, arrays have fixed length.
Q10. Which built-in function returns the length of a slice, array, map, string, or channel?
len and cap are built-ins.
Answer: len() — returns length; cap() returns capacity for slices/arrays.
Q11. Which built-in returns the capacity of a slice or array?
Used to inspect underlying array capacity.
Answer: cap() — returns the capacity of slices and arrays.
Q12. Which composite type maps keys to values?
Unordered key-value store.
Answer: map — maps associate keys to values with fast lookup.
Q13. Fill: Declare a map from string to int: var m
Map type syntax: map[keyType]valueType.
Answer: map[string]int — declares m as a map with string keys and int values.
Q14. Which keyword declares a constant?
Used for compile-time constants.
Answer: const — declares constants in Go.
Q15. Which statement creates a new goroutine?
Lightweight concurrent function call.
Answer: go f() — starts f() in a new goroutine for concurrent execution.
Q16. Which type is used for Unicode code points?
Alias for int32.
Answer: rune — represents a Unicode code point (alias for int32).
Q17. Which statement creates a buffered channel of ints with capacity 5?
make with second argument sets buffer size.
Answer: make(chan int, 5) — creates a buffered channel with capacity 5.
Q18. Which built-in closes a channel?
close(ch) is the syntax.
Answer: close() — use close(ch) to close a channel; only the sender should close.
Q19. Which statement receives a value from channel ch?
Use arrow operator for send/receive.
Answer: v := <-ch — receives a value from ch into v.
Q20. Which statement sends value x to channel ch?
Arrow points from value to channel.
Answer: ch <- x — sends x to channel ch.
Q21. Which keyword declares a new type based on an existing one?
Go uses type keyword for declarations.
Answer: type — e.g., type MyInt int declares a new named type.
Q22. Which composite groups fields under one name?
Go uses struct for grouped fields.
Answer: struct — defines a composite type with named fields.
Q23. Which method set determines behavior for a type and enables interfaces?
Attach functions to types with receiver syntax.
Answer: methods — methods with receivers define behavior and satisfy interfaces.
Q24. Which keyword declares an interface?
Interfaces are satisfied implicitly.
Answer: interface — defines a set of method signatures that types can implement implicitly.
Q25. Which built-in converts a string to an integer (with error)?
Part of strconv package.
Answer: strconv.Atoi — converts string to int and returns (int, error).
Q26. Which package provides JSON encoding/decoding?
Standard library package path.
Answer: encoding/json — provides Marshal and Unmarshal for JSON.
Q27. Which function starts an HTTP server in net/http?
Commonly used in simple web servers.
Answer: http.ListenAndServe — starts an HTTP server on given address with handler.
Q28. Which statement declares an anonymous function and calls it immediately?
Wrap function literal in parentheses then call.
Answer: (func(){ /*...*/ })() — defines and immediately invokes an anonymous function.
Q29. Which built-in recovers from a panic inside a deferred function?
Used with defer to handle panics.
Answer: recover() — returns the value passed to panic if called inside a deferred function.
Q30. Which built-in stops the normal flow and raises a run-time error?
panic triggers stack unwinding and deferred calls.
Answer: panic() — causes a run-time panic; recover can intercept it in deferred functions.
Q31. Which package provides concurrency primitives like WaitGroup?
sync.WaitGroup is commonly used to wait for goroutines.
Answer: sync — provides WaitGroup, Mutex, Once, etc., for synchronization.
Q32. Which package provides atomic operations on numeric types?
Provides functions like AddInt64, LoadUint32.
Answer: sync/atomic — package for low-level atomic memory primitives.
Q33. Which built-in creates a slice with specified length and capacity?
make constructs slices, maps, and channels.
Answer: make([]T, len, cap) — creates a slice of type []T with given length and capacity.
Q34. Which built-in allocates zeroed memory for a type and returns pointer?
new(T) returns *T with zero value.
Answer: new() — new(T) allocates zeroed storage and returns *T; make is for slices, maps, channels.
Q35. Which formatting verb prints a value's Go-syntax representation?
Used with fmt.Printf.
Answer: %#v — prints a Go-syntax representation of the value; %v prints default format.
Q36. Which package provides logging utilities like Println and Fatal?
log.Fatal exits after logging.
Answer: log — provides logging functions and configurable logger objects.
Q37. Which statement checks if key k exists in map m?
Comma-ok idiom returns presence boolean.
Answer: v, ok := m[k] — ok is true if key exists; v is zero value if not present.
Q38. Which statement deletes key k from map m?
Built-in delete function.
Answer: delete(m, k) — removes key k from map m if present.
Q39. Which tool formats Go source code according to standard style?
Standard formatting tool distributed with Go.
Answer: gofmt — formats Go code to the standard style; goimports is a related tool.
Q40. Which command runs tests in the current module or package?
Runs unit tests in _test.go files.
Answer: go test — executes tests in the current package or module.
Q41. Which directive imports a package with a blank identifier to run its init only?
Blank identifier prevents direct use of package name.
Answer: _ "package/path" — imports solely for side-effects (init functions).
Q42. Which import form brings package identifiers into current namespace (discouraged)?
Dot import places exported names in current scope.
Answer: . "package/path" — dot import brings exported identifiers into current namespace (use sparingly).
Q43. Which standard tool tidies go.mod and go.sum files?
Removes unused dependencies and adds missing ones.
Answer: go mod tidy — cleans up module dependencies in go.mod and go.sum.
Q44. Which type represents an unsigned 8-bit integer?
byte is an alias for uint8.
Answer: byte — alias for uint8, commonly used for raw data and bytes.
Q45. Which statement declares a method with pointer receiver for type T?
func (t ) Foo() {}
Pointer receiver uses * before type name.
Answer: *T — func (t *T) Foo() {} declares a method with pointer receiver.
Q46. Which built-in returns the type of a value at runtime (reflection)?
Part of reflect package.
Answer: reflect.TypeOf — returns a reflect.Type describing the dynamic type of the value.
Q47. Which statement creates a new error with formatted message?
fmt.Errorf formats and returns an error.
Answer: fmt.Errorf — creates an error with formatted message (Go 1.13+ supports %w for wrapping).
Q48. Which pattern is idiomatic for returning a value and an error?
Value first, error last is conventional.
Answer: func Foo() (T, error) — idiomatic Go returns value and error, with error as last return value.
Q49. Which statement compiles and runs a Go program in one step?
Runs source files directly.
Answer: go run — compiles and runs the specified Go program files in one step.
Q50. Which practice is recommended for error handling in Go?
Handle errors where they occur; return them upward when appropriate.
Answer: Check and handle errors explicitly — idiomatic Go checks errors and handles or returns them rather than ignoring.
Your Score: 0 / 50

Post a Comment

0 Comments