Find the Output of Go (Golang) Programs - MCQ Questions and Answers

Welcome to Find the Output of Go (Golang) Programs—MCQ Questions and Answers. Here, we present 25 Go programming multiple-choice questions (MCQs) designed to test understanding of Golang concepts such as concurrency, slices, maps, interfaces, error handling, etc. Each snippet includes potential outputs to choose from, along with the correct answer and a brief explanation.

1. What will be the output of the following Go program?

package main
import "fmt"
func main() {
    fmt.Println("Hello, Go!")
}
a) Hello, Go
b) Hello, Go!
c) Compilation error
d) None of the above

2. What will be the output of the following Go program?

package main
import "fmt"
func main() {
    var a int = 10
    var b int = 3
    var c int = a / b
    fmt.Println(c)
}
a) 3
b) 3.33
c) 10/3
d) None of the above

3. What will be the output of the following Go program involving a slice?

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

4. What will be the output of the following Go program using a map?

package main
import "fmt"
func main() {
    m := make(map[string]int)
    m["a"] = 1
    if val, exists := m["b"]; exists {
        fmt.Println(val)
    } else {
        fmt.Println("Key not found")
    }
}
a) 0
b) 1
c) Key not found
d) None of the above

5. What will be the output of the following Go program involving a goroutine?

package main
import (
"fmt"
"time"
)
func printCount(c chan int) {
    num := 0
    for num >= 0 {
        num = <-c
        fmt.Print(num, " ")
    }
}
func main() {
    c := make(chan int)
    go printCount(c)
    for i := 0; i < 5; i++ {
        c <- i
    }
    time.Sleep(time.Millisecond * 1) // wait for goroutine to finish
    fmt.Println("End")
}
a) 0 1 2 3 4 End
b) End 0 1 2 3 4
c) 0 1 2 3 4
d) Random order of numbers from 0 to 4 followed by End

6. What will be the output of the following Go program using an interface and type assertion?

package main
import "fmt"
type I interface {
    Get() int
}
type T struct {
    val int
}
func (t T) Get() int {
    return t.val
}
func main() {
var i I = T{val: 5}
if t, ok := i.(T); ok {
    fmt.Println(t.Get())
    } else {
        fmt.Println("Type assertion failed")
    }
}
a) 5
b) Type assertion failed
c) Error
d) 0

7. What will be the output of the following Go program using pointers?

package main
import "fmt"
func main() {
    a := 10
    b := &a
    *b = 20
    fmt.Println(a)
}
a) 10
b) 20
c) Error
d) None of the above

8. What will be the output of the following Go program using a switch statement?

package main
import "fmt"
func main() {
    x := 1
    switch x {
        case 1:
        fmt.Print("one ")
        fallthrough
        case 2:
        fmt.Print("two ")
        default:
        fmt.Print("many ")
    }
}
a) one
b) one two
c) one two many
d) many

9. What will be the output of the following Go program involving defer?

package main
import "fmt"
func main() {
    fmt.Print("Start ")
    defer fmt.Print("Middle ")
    fmt.Print("End ")
}
a) Start Middle End
b) Start End Middle
c) Middle Start End
d) End Start Middle

10. What will be the output of the following Go program with a struct and method?

package main
import "fmt"
type Person struct {
    Name string
}
func (p *Person) Introduce() {
    fmt.Printf("Hi, I'm %s\n", p.Name)
}
func main() {
    p := &Person{Name: "John"}
    p.Introduce()
}
a) Hi, I'm
b) Hi, I'm John
c) Error
d) None of the above

11. What will be the output of the following Go program that tests string concatenation?

package main
import "fmt"
func main() {
    s := "Go"
    s += "lang"
    fmt.Println(s)
}
a) Go
b) lang
c) Golang
d) Compilation error

12. What will be the output of the following Go program using a simple loop?

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

13. What will be the output of the following Go program that manipulates a slice?

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

14. What will be the output of the following Go program using type conversion?

package main
import "fmt"
func main() {
    var i int = 42
    var f float64 = float64(i)
    var u uint = uint(f)
    fmt.Println(u)
}
a) 42
b) 42.0
c) Error
d) None of the above

15. What will be the output of the following Go program with map access?

package main
import "fmt"
func main() {
    m := map[string]int{"one": 1, "two": 2}
    fmt.Println(m["three"])
}
a) 3
b) 0
c) Error
d) None of the above

16. What will be the output of the following Go program using a custom error?

package main
import (
"fmt"
"errors"
)
func main() {
    err := errors.New("a custom error")
    fmt.Println(err)
}
a) a custom error
b) Error
c) nil
d) None of the above

17. What will be the output of the following Go program that uses defer in a loop?

package main
import "fmt"
func main() {
    for i := 0; i < 3; i++ {
        defer fmt.Print(i, " ")
    }
}
a) 0 1 2
b) 2 1 0
c) 2 2 2
d) None of the above

18. What will be the output of the following Go program with a recursive function?

package main
import "fmt"
func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}
func main() {
    fmt.Println(factorial(5))
}
a) 120
b) 24
c) 5
d) 1

19. What will be the output of the following Go program involving an interface?

package main
import "fmt"
type Geometry interface {
    Area() float64
}
type Square struct {
    side float64
}
func (s Square) Area() float64 {
    return s.side * s.side
}
func printArea(g Geometry) {
    fmt.Println(g.Area())
}
func main() {
    s := Square{side: 5}
    printArea(s)
}
a) 25
b) 5
c) Error
d) None of the above

20. What will be the output of the following Go program using goroutines and channels?

package main
import (
"fmt"
"time"
)
func sendMessage(c chan string) {
    time.Sleep(1 * time.Second)
    c <- "Hello Channel!"
}
func main() {
    messages := make(chan string)
    go sendMessage(messages)
    msg := <-messages
    fmt.Println(msg)
}
a) Hello Channel!
b) No output
c) Error
d) None of the above

21. What will be the output of the following Go program using string manipulation?

package main
import (
"fmt"
"strings"
)
func main() {
    fmt.Println(strings.Trim("!!!Hello, Go!!!", "!"))
}
a) Hello, Go
b) !!!Hello, Go
c) Hello, Go!!!
d) None of the above

22. What will be the output of the following Go program using slice expansion?

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

23. What will be the output of the following Go program that uses a struct?

package main
import "fmt"
type Point struct {
    X, Y int
}
func main() {
    p := Point{1, 2}
    q := &Point{1, 2}
    fmt.Println(p.X == q.X && p.Y == q.Y)
}
a) true
b) false
c) Error
d) None of the above

24. What will be the output of the following Go program using constants?

package main
import "fmt"
func main() {
    const Pi float64 = 3.14159
    const zero = 0.0
    fmt.Println(Pi / zero)
}
a) 0
b) Infinity
c) NaN
d) Error

25. What will be the output of the following Go program using multiple goroutines?

    package main
    import (
    "fmt"
    "sync"
    )
    func main() {
    var wg sync.WaitGroup
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func(x int) {
            defer wg.Done()
            fmt.Print(x, " ")
            }(i)
        }
        wg.Wait()
        fmt.Println("Done")
    }
a) 0 1 2 3 4 Done
b) Done 0 1 2 3 4
c) 4 3 2 1 0 Done
d) Random order of numbers from 0 to 4 followed by Done

Comments