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 implementing it?
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. The method returns "Woof!" exactly, so printing s.Speak() outputs "Woof!".
❓ Predict Output
intermediate2:00remaining
Interface value nil check
What will be printed by this Go program that checks if an interface value is nil?
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 value is nil only if both type and value are nil.
✗ Incorrect
The interface r holds a type *File and a nil value. Since the type is not nil, r != nil, so it prints "r is not nil".
🔧 Debug
advanced2:00remaining
Why does this interface implementation fail?
This Go code tries to assign a struct to an interface variable but fails to compile. Why?
Go
package main import "fmt" type Walker interface { Walk() } type Person struct {} func (p Person) walk() { fmt.Println("Walking") } func main() { var w Walker = Person{} w.Walk() }
Attempts:
2 left
💡 Hint
Check method names and case sensitivity in Go.
✗ Incorrect
The method is named 'walk' with lowercase 'w', but interface requires 'Walk' with uppercase 'W'. Go is case-sensitive, so Person does not implement Walker.
📝 Syntax
advanced2:00remaining
Which option correctly implements this interface?
Given this interface, which option correctly implements it without syntax errors?
Go
type Printer interface {
Print(msg string) string
}Attempts:
2 left
💡 Hint
Check method signature matches interface exactly.
✗ Incorrect
Option D matches the interface method signature exactly: method name, parameter, and return type. Others differ in signature or receiver.
🚀 Application
expert2:00remaining
How many items in the slice after interface assignment?
Consider this Go code. How many elements does the slice 'items' contain after execution?
Go
package main import "fmt" type Item interface { ID() int } type Product struct { id int } func (p Product) ID() int { return p.id } func main() { var items []Item p1 := Product{id: 1} p2 := Product{id: 2} items = append(items, p1) items = append(items, &p2) fmt.Println(len(items)) }
Attempts:
2 left
💡 Hint
Both Product value and pointer implement Item interface.
✗ Incorrect
Both p1 (value) and &p2 (pointer) implement the Item interface, so both can be appended. The slice length is 2.