Debug School

Akanksha
Akanksha

Posted on

Top 30 Go Interview Questions with Answers

1. What is Go?

a) A dynamic scripting language
b) A compiled, statically typed language
c) A markup language
d) A database management system
Answer: b

2. Which of the following is a statically typed language?

a) Python
b) Ruby
c) Go
d) JavaScript
Answer: c

3. What is a channel in Go?

a) A way to communicate between goroutines
b) A type of data structure
c) A pointer to a variable
d) A file handle
Answer: a

4. How do you create a buffered channel in Go?

a) make(chan int, 5)
b) chan := new(chan int, 5)
c) bufferedChan(5)
d) channel := make(chan int)
Answer: a

5. Which keyword is used to declare a new variable in Go?

a) var
b) new
c) let
d) const
Answer: a

6. What is a closure in Go?

a) A function that takes no arguments
b) A function that returns a channel
c) A function that references variables from its surrounding context
d) A built-in Go type
Answer: c

7. What does the main function in a Go program do?

a) It is not required in Go programs.
b) It is where you declare global variables.
c) It is the entry point of the program.
d) It defines the package name.
Answer: c

8. Which package provides support for working with JSON in Go?

a) encoding/json
b) jsonutils
c) jsonencode
d) jsonlib
Answer: a

9. How do you define a constant in Go?

a) Using the const keyword
b) Using the let keyword
c) Using the final keyword
d) Constants are not allowed in Go.
Answer: a

10. How do you marshal a Go struct into a JSON object?

a) json.Marshal(struct)
b) json.Encode(struct)
c) json.Serialize(struct)
d) struct.ToJSON()
Answer: a

11. Which data type represents a single character in Go?

a) char
b) byte
c) character
d) rune
Answer: d

12. What is the purpose of the sync package in Go?

a) To handle synchronization of goroutines
b) To perform file I/O operations
c) To manage network connections
d) To parse command-line arguments
Answer: a

13. What is the purpose of the init function in Go?

a) To initialize global variables
b) To specify package dependencies
c) To handle errors
d) To declare constants
Answer: b

14. What is the purpose of the defer keyword in Go?

a) To defer the execution of a function until the program exits
b) To defer the execution of a function until a specified time
c) To delay the execution of a function until the surrounding function returns
d) To skip the execution of a function
Answer: c

15. How do you import a package in Go?

a) Using the import keyword
b) Using the require keyword
c) Using the use keyword
d) Go does not support importing packages.
Answer: a

16. How do you read data from the standard input in Go?

a) fmt.Scanln()
b) fmt.Readln()
c) fmt.Read()
d) fmt.Scan()
Answer: d

17. Which data structure in Go is similar to an array but has a dynamic size?

a) Array
b) Slice
c) List
d) Map
Answer: b

18. How do you write data to the standard output in Go?

a) fmt.Print()
b) fmt.Println()
c) fmt.Write()
d) fmt.WriteToStdout()
Answer: b

19. What is the difference between a map and a slice in Go?

a) A map is a collection of key-value pairs, while a slice is an ordered list of elements.
b) A map is fixed in size, while a slice can grow dynamically.
c) A map is used for iteration, while a slice is used for random access.
d) A map can only store integers, while a slice can store any type of data.
Answer: a

20. What is the purpose of the recover function in Go?

a) To handle panics and recover from them
b) To recover memory
c) To handle errors
d) To recover deleted files
Answer: a

21. How do you declare and initialize a map in Go?

a) myMap := map{}
b) myMap := make(map)
c) myMap := map[string]int{}
d) myMap := new(map)
Answer: c

22. How do you perform unit testing in Go?

a) Use the go test command
b) Use the go run command
c) Use the go unit-test command
d) Use the go check command
Answer: a

23. What is a goroutine in Go?

a) A thread
b) A function
c) A channel
d) A synchronization primitive
Answer: a

24. Which keyword is used to create a structure in Go?

a) struct
b) class
c) type
d) new
Answer: c

25. How do you start a new goroutine in Go?

a) go myFunction()
b) start myFunction()
c) run myFunction()
d) thread myFunction()
Answer: a

26. How do you define a method on a struct in Go?

a) func (s *MyStruct) methodName() {}
b) method (s MyStruct) {}
c) function (s *MyStruct) methodName() {}
d) method (MyStruct) {}
**Answer: a
*

27. What is a defer statement used for in Go?

a) To create a new variable
b) To handle panics
c) To delay the execution of a function until the surrounding function returns
d) To start a new goroutine
Answer: c

28. What is the purpose of the panic function in Go?

a) To handle panics and recover from them
b) To print an error message and exit the program
c) To signal a fatal error
d) To terminate a goroutine
Answer: c

29. How do you handle errors in Go?

a) Using try-catch blocks
b) Using if-else statements
c) Using the panic function
d) Using the error type and if err != nil checks
Answer: d

30. How do you convert a string to an integer in Go?

a) strconv.Atoi()
b) strconv.ParseInt()
c) strconv.ToInt()
d) string.ToInt()
Answer: a

Top comments (0)