Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()) }
Attempts:
2 left
💡 Hint
Check the Speak method's return value and how it's printed.
✗ Incorrect
The Dog struct implements the Speaker interface by defining Speak() string. Calling s.Speak() returns "Woof!" exactly as defined.
❓ Predict Output
intermediate2: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") } }
Attempts:
2 left
💡 Hint
An interface holding a nil pointer is not itself nil.
✗ Incorrect
The interface r holds a *File pointer which is nil, but the interface itself is not nil, so the else branch runs.
🔧 Debug
advanced2: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") }
Attempts:
2 left
💡 Hint
Check method names and signatures carefully.
✗ Incorrect
The Writer interface requires a Write method, but Printer only has Print method, so it does not implement Writer.
🧠 Conceptual
advanced2: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 }
Attempts:
2 left
💡 Hint
Interface embedding combines all methods from embedded interfaces.
✗ Incorrect
Interface C embeds A and B, so it requires all methods from both interfaces: Foo() and Bar().
❓ Predict Output
expert2: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()) }
Attempts:
2 left
💡 Hint
Check if Cat value implements Speaker when Speak has pointer receiver.
✗ Incorrect
Speak has pointer receiver (*Cat), so Cat value does not implement Speaker interface. Assignment fails to compile.