Go course

InterfacesQuiz: Interfaces

Go

Display

Theme

Interfaces · Quiz

Quiz: Interfaces

Test what you know about interfaces, type assertions, type switches, and any. Answer every question in the right-hand panel.

  1. Question 1 of 6How does a Go type declare that it implements an interface?
  2. Question 2 of 6What happens when the following code runs?
    var x any = "halo"
    n := x.(int)
    fmt.Println(n)
  3. Question 3 of 6What are the values of n and ok after this code?
    var x any = "halo"
    n, ok := x.(int)
  4. Question 4 of 6What does the following program print?
    type Kopi struct{ Nama string }
    
    func (k Kopi) String() string { return "☕ " + k.Nama }
    
    func main() {
    	fmt.Println(Kopi{Nama: "Latte"})
    }
  5. Question 5 of 6Inside the following type switch, what is the type of the variable v in the string case?
    switch v := x.(type) {
    case string:
    	// di sini
    }
  6. Question 6 of 6Which interface design guideline is recommended in Go?