0
0
Goprogramming~20 mins

Interface definition in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of interface method call
What is the output of this Go program that uses an interface and a struct?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
    return "Woof!"
}
func main() {
    var s Speaker = Dog{}
    fmt.Println(s.Speak())
}
AWoof!
BWoof
Cspeak
DCompilation error
Attempts:
2 left
💡 Hint
Check the Speak method's return value and how it's printed.
Predict Output
intermediate
2:00remaining
Interface variable nil check
What will be printed by this Go program regarding interface nil checks?
Go
package main
import "fmt"
type Reader interface {
    Read() string
}
type File struct {}
func (f *File) Read() string {
    return "data"
}
func main() {
    var r Reader
    var f *File = nil
    r = f
    if r == nil {
        fmt.Println("r is nil")
    } else {
        fmt.Println("r is not nil")
    }
}
ARuntime panic
Br is nil
CCompilation error
Dr is not nil
Attempts:
2 left
💡 Hint
An interface holding a nil pointer is not itself nil.
🔧 Debug
advanced
2:00remaining
Why does this interface assignment fail?
This code fails to compile. What is the cause of the error?
Go
package main
import "fmt"
type Writer interface {
    Write(data string)
}
type Printer struct {}
func (p Printer) Print(data string) {
    fmt.Println(data)
}
func main() {
    var w Writer = Printer{}
    w.Write("Hello")
}
APrinter does not implement Writer because method has wrong parameter type
BPrinter does not implement Writer because method name is Print, not Write
CInterface Writer cannot be assigned to struct Printer
DMissing import for fmt package
Attempts:
2 left
💡 Hint
Check method names and signatures carefully.
🧠 Conceptual
advanced
2:00remaining
Interface embedding behavior
Given these interface definitions, which statement is true about interface C?
Go
type A interface {
    Foo()
}
type B interface {
    Bar()
}
type C interface {
    A
    B
}
AC requires only Foo() method to be implemented
BC requires only Bar() method to be implemented
CC requires both Foo() and Bar() methods to be implemented
DC does not require any methods to be implemented
Attempts:
2 left
💡 Hint
Interface embedding combines all methods from embedded interfaces.
Predict Output
expert
2:00remaining
Interface method call with pointer vs value receiver
What is the output of this Go program?
Go
package main
import "fmt"
type Speaker interface {
    Speak() string
}
type Cat struct {}
func (c *Cat) Speak() string {
    return "Meow"
}
func main() {
    var s Speaker
    c := Cat{}
    s = c
    fmt.Println(s.Speak())
}
ACompilation error: Cat does not implement Speaker
BRuntime panic
CMeow
DEmpty string
Attempts:
2 left
💡 Hint
Check if Cat value implements Speaker when Speak has pointer receiver.