Golang Interfaces Quiz
35 comprehensive questions on Golang interfaces, covering interface basics, implicit implementation, empty interface, type assertions, and type switches — with 18 code examples demonstrating interface usage and polymorphism.
Question 1
What is an interface in Golang?
Question 2
How do you declare an interface in Golang?
Question 3
What will this basic interface example print?
package main
import "fmt"
type Speaker interface {
speak()
}
type Dog struct{}
func (d Dog) speak() {
fmt.Println("Woof!")
}
func main() {
var s Speaker = Dog{}
s.speak()
}Question 4
What is implicit interface implementation in Golang?
Question 5
What is the empty interface interface{}?
Question 6
What will this empty interface example print?
package main
import "fmt"
func printValue(v interface{}) {
fmt.Printf("Type: %T, Value: %v\n", v, v)
}
func main() {
printValue(42)
printValue("hello")
printValue([]int{1, 2, 3})
}Question 7
What is a type assertion in Golang?
Question 8
What will this type assertion example print?
package main
import "fmt"
func main() {
var i interface{} = "hello world"
s := i.(string)
fmt.Println(s)
}Question 9
What happens if a type assertion fails?
Question 10
What is the comma ok idiom for type assertions?
Question 11
What will this safe type assertion example print?
package main
import "fmt"
func main() {
var i interface{} = 42
if s, ok := i.(string); ok {
fmt.Println("String:", s)
} else {
fmt.Println("Not a string")
}
}Question 12
What is a type switch in Golang?
Question 13
What will this type switch example print?
package main
import "fmt"
func printType(i interface{}) {
switch v := i.(type) {
case int:
fmt.Printf("Integer: %d\n", v)
case string:
fmt.Printf("String: %s\n", v)
default:
fmt.Printf("Other: %T\n", v)
}
}
func main() {
printType(42)
printType("hello")
printType(3.14)
}Question 14
Can interfaces be embedded in structs?
Question 15
What will this embedded interface example do?
package main
import "fmt"
type Reader interface {
read()
}
type Writer interface {
write()
}
type ReadWriter interface {
Reader
Writer
}
type File struct{}
func (f File) read() { fmt.Println("reading") }
func (f File) write() { fmt.Println("writing") }
func main() {
var rw ReadWriter = File{}
rw.read()
rw.write()
}Question 16
What is the zero value of an interface variable?
Question 17
What happens when you call a method on a nil interface?
Question 18
What will this nil interface example do?
package main
type Speaker interface {
speak()
}
func main() {
var s Speaker
s.speak() // This will panic
}Question 19
What is interface satisfaction?
Question 20
Do pointer and value receivers satisfy different interfaces?
Question 21
What will this method set example show?
package main
import "fmt"
type Mover interface {
move()
}
type Car struct{}
func (c Car) move() {
fmt.Println("Car moving")
}
func main() {
var m Mover = Car{}
m.move()
var m2 Mover = &Car{}
m2.move()
}Question 22
What is the difference between interface{} and any?
Question 23
What is interface composition?
Question 24
What will this interface composition example do?
package main
import "fmt"
type Reader interface {
Read([]byte) (int, error)
}
type Writer interface {
Write([]byte) (int, error)
}
type ReadWriter interface {
Reader
Writer
}
func main() {
fmt.Println("ReadWriter requires both Read and Write methods")
}Question 25
Can you type assert to an interface?
Question 26
What will this interface assertion example do?
package main
import "fmt"
type Stringer interface {
String() string
}
type Printer interface {
Print()
}
type Document struct{}
func (d Document) String() string { return "document" }
func (d Document) Print() { fmt.Println("printing") }
func main() {
var i interface{} = Document{}
if s, ok := i.(Stringer); ok {
fmt.Println(s.String())
}
if p, ok := i.(Printer); ok {
p.Print()
}
}Question 27
What is the reflect package's relationship to interfaces?
Question 28
What is a common pitfall with interface{}?
Question 29
What is the main benefit of interfaces in Golang?
Question 30
What is the difference between error interface and other interfaces?
Question 31
What will this error interface example do?
package main
import "fmt"
type MyError struct {
msg string
}
func (e MyError) Error() string {
return e.msg
}
func doSomething() error {
return MyError{"something went wrong"}
}
func main() {
if err := doSomething(); err != nil {
fmt.Println(err)
}
}Question 32
Can interfaces have methods with the same name?
Question 33
What is the method set of an interface?
Question 34
What is duck typing?
Question 35
What is the key principle for effective interface design in Golang?
