Go (Golang) Online Quiz

Welcome to our comprehensive Go (Golang) online quiz, designed to test your skills in this powerful programming language developed by Google. Go is known for its efficiency, excellent concurrency support, and simplicity. This quiz covers everything from basic syntax and control structures to more complex concepts like goroutines and channels. Whether you are preparing for a job interview or just looking to test your Go programming prowess, these questions will challenge you and help deepen your understanding of the language.

1. What is the primary use of the defer statement in Go?

a) To postpone the execution of a function until the surrounding function returns
b) To execute a function immediately
c) To handle errors
d) To define constants

2. How do you declare a slice in Go?

var mySlice []int
a) var mySlice []int
b) mySlice := []int{}
c) var mySlice int[]
d) []int mySlice

3. What does the following Go code output?

package main
import "fmt"
func main() {
    fmt.Println(len("Hello, World!"))
}
a) 12
b) 13
c) 14
d) Error

4. Which keyword is used to create a new goroutine in Go?

a) func
b) go
c) goroutine
d) async

5. What is the output of the following Go code?

package main
import "fmt"
func main() {
    var a = "initial"
    fmt.Println(a)
}
a) initial
b) nil
c) ""
d) Error

6. How do you declare a map in Go with strings as keys and ints as values?

a) var map[string]int
b) map[string]int{}
c) var myMap map[string]int
d) make(map[string]int)

7. What will the following Go code output?

package main
import "fmt"
func main() {
    s := make([]string, 3)
    fmt.Println(s)
}
a) ["" "" ""]
b) [nil nil nil]
c) []
d) [0 0 0]

8. Which function is used to concatenate strings in Go?

a) concat()
b) append()
c) Join()
d) + operator

9. How do you handle errors in Go?

_, err := someFunction()
if err != nil {
    fmt.Println("Error:", err)
}
a) Using the error package
b) Using a special error handling function
c) Checking if an error variable is nil
d) Using try and catch

10. What is the zero value of a pointer in Go?

a) 0
b) nil
c) undefined
d) None of the above

11. Which package in Go is universally used to implement formatted I/O operations, similar to C's printf and scanf?

a) os
b) sys
c) fmt
d) io

12. What does the following Go code output?

package main
import "fmt"
func main() {
    i := 10
    fmt.Println(i)
    increment(&i)
    fmt.Println(i)
}
func increment(num *int) {
    *num++
}
a) 10
b) 10
c) Error
d) 11

13. How do you specify a multi-line comment in Go?

a) // This is a comment
b) /* This is a comment */
c) ### This is a comment ###
d) -- This is a comment --

14. What is the correct way to define a new struct in Go?

type Person struct {
    Name string
    Age  int
}
a) type Person struct { Name string, Age int }
b) struct Person { Name string, Age int }
c) type Person { Name string; Age int }
d) type Person struct { Name string; Age int }

15. What does the interface{} type represent in Go?

a) An array of interfaces
b) A specific interface with certain methods
c) An empty interface that may hold values of any type
d) A null interface

16. How can you convert an integer to a string in Go?

a) Using toString() function
b) Using Itoa function from the strconv package
c) Using the (string) type conversion
d) Using the stringify() function

17. Which method can be used to compare two slices in Go?

a) Compare()
b) Equals()
c) reflect.DeepEqual()
d) ==

18. What is the built-in function in Go used to retrieve the length of a slice?

a) len()
b) length()
c) size()
d) getSize()

19. How do you create a channel in Go?

ch := make(chan int)
a) chan int ch = new()
b) ch := new(chan int)
c) ch := make(chan int)
d) create chan int

20. What is the conventional way to ignore a return value in Go?

a) Use an underscore (_) to ignore
b) Use the ignore keyword
c) Just omit the variable
d) Use a dash (-) to ignore

21. What will the following Go code output?

package main
import "fmt"
func main() {
    defer fmt.Println("world")
    fmt.Println("hello")
}
a) world hello
b) hello world
c) hello
d) world

22. How do you handle panics in Go?

a) Using panic()
b) Using catch()
c) Using defer along with recover()
d) Using try()

23. What is the output of the following Go code?

package main
import "fmt"
func main() {
    fmt.Println("start")
    for i := 0; i < 3; i++ {
        defer fmt.Println(i)
    }
    fmt.Println("end")
}
a) start 0 1 2 end
b) start end 2 1 0
c) start end 0 1 2
d) 0 1 2 start end

24. What built-in package in Go is most often used for handling I/O operations, like reading from or writing to files?

a) fmt
b) io
c) bufio
d) os

25. How can you ensure a function always runs at the end of a Go program?

a) Using the final keyword
b) Using the last() function
c) Using defer in the main function
d) Using the end keyword

Comments