Logo

Golang Questions Set 10:

Quiz Mode

What is the output of the following code?

package main

import "fmt"


func main() {

   x := []int{1, 2, 3}

   y := x[:2]

   y[0] = 4

   fmt.Println(x[0])

}

Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

   x := 1

   addOne(&x)

   fmt.Println(x)

}


func addOne(y *int) {

   *y = *y + 1

}

Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

   ch := make(chan int)

   go func() {

       ch <- 1

   }()

   fmt.Println(<-ch)

}

Solution:

What is an interface in Go?

1
2
3
4

Solution:

What is the output of the following code?

go

Copy code

package main

import "fmt"


func main() {

   var x *int

   fmt.Println(*x)

}

1
2
3
4

Solution:

What is the output of the following code?

package main

import "fmt"

type Employee struct {

   ID int

}

func main() {

   e := Employee{ID: 1}

   f := e

   f.ID = 2

   fmt.Println(e.ID)

}

Solution:

Which of the following is not a valid way to declare an interface in Go?

1
2
3
4

Solution:

What is the output of the following code?

package main


import (

"fmt"

)


func add(numbers ...int) int {

sum := 0

for _, n := range numbers {

sum += n

}

return sum

}


func main() {

nums := []int{1, 2, 3, 4}

fmt.Println(add(nums...))

}


1
2
3
4

Solution:

What is the output of the following code?

package main

import "fmt"


type Employee struct {

   ID int

}


func main() {

   e := Employee{ID: 1}

   fmt.Println(e)

   changeID(&e)

   fmt.Println(e)

}


func changeID(e *Employee) {

   e.ID = 2

}

1
2
3
4

Solution:

What is the output of the following code?

package main


import (

   "fmt"

   "time"

)

func main() {

   done := make(chan bool)

   go func() {

       time.Sleep(1 * time.Second)

       done <- true

   }()

   fmt.Println("Waiting...")

   <-done

   fmt.Println("Done")

}

1
2
3
4

Solution: