yoklainterview sim

Backend Go Interfaces Generics Interview Questions

75 verified Backend Go Interfaces Generics interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Go Interfaces GenericsDifficulty 1
Which statement about interface satisfaction in Go is correct?
  • aA type satisfies an interface automatically once its method set includes all of the interface's methods.
  • bA type must declare which interfaces it implements using an implements keyword.
  • cA type satisfies an interface only if it imports the interface's defining package.
  • dA type satisfies an interface only if its methods are declared in the same file as the interface.
Explanation:Go interfaces are satisfied implicitly (structural typing): there is no implements keyword. If a type's method set contains every method declared in an interface, it satisfies that interface automatically, regardless of package or file location.
Go Interfaces GenericsDifficulty 2
type Animal interface { Speak() string }
type Dog struct{}
func (d *Dog) Speak() string { return "Woof" }

func main() {
    var a Animal = Dog{}
    fmt.Println(a.Speak())
}

What happens when this code is compiled?
  • aIt compiles and prints "Woof".
  • bIt fails to compile: Dog does not implement Animal.
  • cIt compiles but panics at runtime when Speak is called.
  • dIt compiles and prints an empty string.
Explanation:Speak has a pointer receiver, so it belongs to (*Dog)'s method set, not Dog's. Assigning a Dog value (not &Dog{}) to the Animal variable fails to compile with an error like "Dog does not implement Animal (Speak method has pointer receiver)".
Go Interfaces GenericsDifficulty 3
For a type T whose methods are all declared with value receivers, which statement about the method sets of T and *T is correct?
  • aT's method set includes only pointer-receiver methods.
  • b*T's method set includes only pointer-receiver methods and excludes T's value-receiver methods.
  • c*T's method set has both value- and pointer-receiver methods; T's has only the value ones.
  • dT and *T always have exactly the same method set, with no exceptions.
Explanation:The method set of T is the union of value-receiver and pointer-receiver methods declared on T, while the method set of T contains only the value-receiver methods. This asymmetry is why a T often satisfies interfaces that T alone does not.
Go Interfaces GenericsDifficulty 1
type Status int

const Active Status = 1

func (s Status) String() string {
    return "active"
}

func main() {
    var s Status = Active
    fmt.Printf("status: %v\n", s)
}

What does this print?
  • astatus: 1
  • bIt fails to compile.
  • cIt panics at runtime.
  • dstatus: active
Explanation:Status implements fmt.Stringer via its String() method. When %v formats a value that satisfies fmt.Stringer, fmt calls String() instead of printing the underlying int, so the output uses the returned string.
Go Interfaces GenericsDifficulty 1
In Go, what is any and how does it relate to types?
  • aA special generic-only keyword usable exclusively inside type parameter lists.
  • bAn alias for interface{}, the empty interface, which has no methods and is satisfied by every type.
  • cA built-in type that can only hold numeric values.
  • dA keyword that declares a variable's type will be inferred once and fixed forever.
Explanation:any was added as an alias for interface{}. Because the empty interface declares zero methods, every type's method set trivially satisfies it, so a variable of type any can hold a value of any type.
Go Interfaces GenericsDifficulty 2
var i any = "hello"
n := i.(int)
fmt.Println(n)

What happens when this code runs?
  • aIt prints 0.
  • bIt compiles, and n is nil.
  • cIt prints "hello" converted to an int.
  • dIt panics at runtime.
Explanation:The single-value form of a type assertion (i.(int)) panics if the assertion fails. Since i's dynamic type is string, asserting int panics with a message like "interface conversion: interface {} is string, not int".

Test yourself against the 3300-question Backend bank.

Start interview