Golang Coding Online Test

Welcome to Golang Coding Online Test!. Here, we will present 20 MCQs (Multiple-Choice Questions) containing simple code snippets to test your coding and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

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

package main
import "fmt"
func main() {
    fmt.Println(5 ^ 2)
}
a) 7
b) 10
c) 2
d) 5

2. What will the following Go code print?

package main
import "fmt"
func main() {
    s := make([]int, 0, 5)
    s = append(s, 1, 2, 3, 4, 5, 6)
    fmt.Println(len(s))
}
a) 5
b) 6
c) 0
d) 1

3. What does the following Go code output?

package main
import "fmt"
func main() {
    var a = "g"
    var b = "o"
    var c = "g" + "o"
    fmt.Println(c == a+b)
}
a) true
b) false
c) compilation error
d) runtime error

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

package main
import "fmt"
func main() {
x := []int{1, 2, 3}
y := x[:0]
fmt.Println(len(y))
}
a) 0
b) 1
c) 2
d) 3

5. What does the following Go code print?

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

6. What will be the output of the following Go code?

package main
import "fmt"
func main() {
    i := 10
    defer func() {
        fmt.Println(i)
        }()
        i++
    }
a) 10
b) 11
c) 0
d) 1

7. What does this Go code output?

package main
import "fmt"
func main() {
    var x interface{} = 7
    fmt.Println(x.(int))
}
a) 7
b) type mismatch error
c) 0
d) panic

8. What will the following Go code snippet output?

package main
import "fmt"
func main() {
x := []int{1, 2, 3}
y := x[:2]
z := x[1:]
y[1] = 20
z[0] = 10
fmt.Println(x)
}
a) [1, 20, 3]
b) [1, 10, 3]
c) [1, 10, 20]
d) [1, 20, 10]

9. What does the following Go code print?

package main
import "fmt"
func main() {
    x := 5
    fmt.Println(x << 1)
}
a) 10
b) 5
c) 2.5
d) 20

10. What is the result of executing the following Go code?

package main
import "fmt"
func main() {
m := map[int]string{1: "a", 2: "b", 3: "c"}
delete(m, 2)
fmt.Println(m[2])
}
a) a
b) b
c) c
d) ""

11. What will this Go function output?

package main
import "fmt"
func main() {
    fmt.Println(f() + g())
}
func f() int {
    fmt.Print("f")
    return 0
}
func g() int {
    fmt.Print("g")
    return 1
}
a) fg1
b) gf1
c) f0g1
d) g0f1

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

package main
import "fmt"
func main() {
    s := struct {
        name string
    }{"Go"}
    p := &s
    p.name = "Golang"
    fmt.Println(s.name)
}
a) Go
b) Golang
c) Compilation error
d) Runtime error

13. What does the following Go code return?

package main
import "fmt"
func main() {
a := [5]int{1, 2, 3, 4, 5}
b := a[1:4]
fmt.Println(len(b), cap(b))
}
a) 3, 4
b) 3, 5
c) 4, 5
d) 3, 3

14. What is the output of this Go program?

package main
import "fmt"
func main() {
x := map[string]int{"one": 1, "two": 2}
if val, ok := x["three"]; ok {
    fmt.Println(val)
    } else {
        fmt.Println("Key not found")
    }
}
a) 0
b) 1
c) Key not found
d) nil

15. What error, if any, does the following Go code produce?

package main
import "fmt"
func main() {
    defer fmt.Println("Done")
    panic("An error occurred")
    fmt.Println("This won't print")
}
a) Syntax error
b) Runtime panic
c) Compilation error
d) No error

16. What does this Go code output?

package main
import "fmt"
func main() {
    ch := make(chan int)
    go func() {
        ch <- 1
        ch <- 2
        close(ch)
        }()
        for i := range ch {
            fmt.Println(i)
        }
    }
a) 1 2
b) 1
c) 1 2 and then a panic
d) Nothing

17. What will this Go function output when called?

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

18. What is the effect of using a goroutine in Go?

package main
import "fmt"
func main() {
    go fmt.Println("Hello from a goroutine")
    fmt.Println("Hello from main")
}
a) It causes the program to execute in a single thread.
b) It creates a new thread to run concurrent code.
c) It runs a function concurrently with other functions.
d) It synchronizes multiple functions to run in order.

19. What will be the output of the following Go function?

package main
import "fmt"
import "strings"
func main() {
    fmt.Println(strings.Trim("!!Hello, Gophers!!", "!"))
}
a) Hello, Gophers
b) !!Hello, Gophers!!
c) Hello, Gophers!!
d) !!Hello, Gophers

20. Given the following Go code, what will be printed?

package main
import "fmt"
func main() {
    a, b := 10, 20
    swap(&a, &b)
    fmt.Println(a, b)
}
func swap(x, y *int) {
    *x, *y = *y, *x
}
a) 10 20
b) 20 10
c) 0 0
d) The program will not compile

Comments