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 to call a method?
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 implementation and what it returns.
✗ Incorrect
The Dog type implements the Speaker interface by defining Speak() string. The method returns "Woof!" exactly, so printing s.Speak() outputs "Woof!".
🧠 Conceptual
intermediate1:30remaining
Why use interfaces in Go?
Which of the following is the main reason to use interfaces in Go?
Attempts:
2 left
💡 Hint
Think about how interfaces allow different types to be used interchangeably.
✗ Incorrect
Interfaces in Go allow different types to implement the same set of methods, enabling polymorphism and flexible code that can work with any type implementing the interface.
🔧 Debug
advanced2:30remaining
Identify the runtime error with interface nil check
What error will this Go program produce when run?
Go
package main import "fmt" type Reader interface { Read() string } type File struct {} func (f *File) Read() string { return "file content" } func main() { var r Reader var f *File = nil r = f if r == nil { fmt.Println("Reader is nil") } else { fmt.Println(r.Read()) } }
Attempts:
2 left
💡 Hint
Check how interface values with nil underlying pointers behave in comparisons.
✗ Incorrect
The interface r holds a *File pointer which is nil, but the interface itself is not nil. So r == nil is false, and calling r.Read() works because the method has a receiver pointer and does not dereference the pointer inside the method. It returns "file content".
📝 Syntax
advanced2:00remaining
Which code snippet correctly implements an interface?
Given interface Writer with method Write(string) error, which option correctly implements it?
Go
type Writer interface {
Write(s string) error
}Attempts:
2 left
💡 Hint
Check method signature matches exactly the interface method.
✗ Incorrect
Option D defines Write with receiver File and parameter string, returning error, matching the interface exactly. A misses the string parameter. C has wrong parameter type. D is a function, not a method.
🚀 Application
expert3:00remaining
How many types implement this interface?
Given the interface and types below, how many distinct types implement the interface Printable?
Go
package main import "fmt" type Printable interface { Print() } type A struct {} func (a A) Print() { fmt.Println("A") } type B struct {} func (b *B) Print() { fmt.Println("B") } type C struct {} func (c C) Print() { fmt.Println("C") } func main() {}
Attempts:
2 left
💡 Hint
Remember that in Go, types with pointer receiver methods also implement the interface via automatic address taking.
✗ Incorrect
A has Print() with value receiver, so A implements Printable. B has Print() with pointer receiver, so B implements Printable (Go automatically uses &b.Print()). C has Print() with value receiver, so C implements Printable. So three types implement Printable: A, B, and C.