Logo

Golang Questions Set 20:

Quiz Mode

Which keyword is used in Go to implement interfaces?

1
2
3
4

Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

  a := make([]int, 3, 6)

  fmt.Println(len(a))

}


Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

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

  b := []int{4, 5, 6}

  a = append(a, b...)

  fmt.Println(a)

}


1
2
3
4

Solution:

What is the output of the following code?

package main

import "fmt"


func main() {

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

   b := []int{4, 5, 6}

   copy(a, b)

   fmt.Println(a, b)

}

1
2
3
4

Solution:

What is the output of the following code?

go

Copy code

package main

import "fmt"


type myInt int


func (mi myInt) double() {

   mi *= 2

}


func main() {

   mi := myInt(2)

   mi.double()

   fmt.Println(mi)

}

1
2
3
4

Solution:

What is an anonymous field in Go?

1
2
3
4

Solution:

Which of the following is true about Go's interfaces?

1
2
3
4

Solution:

What is a pointer receiver in Go?

1
2
3
4

Solution:

Define a struct called "Rectangle" with properties "length" and "width". Create a method called "area" that returns the area of the rectangle. Implement this method using pointer receivers.

1
2
3
4

Solution:

Define a struct called "Person" with properties "name" and "age". Create a method called "greet" that takes no arguments and returns a string. The string should say "Hello, my name is [name] and I am [age] years old." Implement this method using value receivers.

1
2
3
4

Solution: