Challenge - 5 Problems
Empty Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of assigning different types to empty interface
What is the output of this Go program?
Go
package main import "fmt" func main() { var x interface{} x = 42 fmt.Printf("%T %v\n", x, x) x = "hello" fmt.Printf("%T %v\n", x, x) }
Attempts:
2 left
💡 Hint
Empty interface can hold any type, and %T prints the dynamic type.
✗ Incorrect
The empty interface can hold any value. When printing with %T, it shows the actual type stored, so first int then string.
🧠 Conceptual
intermediate1:30remaining
Understanding empty interface usage
Which statement about the empty interface in Go is TRUE?
Attempts:
2 left
💡 Hint
Think about what 'empty interface' means in Go.
✗ Incorrect
The empty interface has zero methods, so all types implement it and can be assigned to it.
❓ Predict Output
advanced2:00remaining
Output of type assertion with empty interface
What is the output of this Go program?
Go
package main import "fmt" func main() { var x interface{} = 3.14 v, ok := x.(int) fmt.Println(v, ok) }
Attempts:
2 left
💡 Hint
Type assertion fails if the dynamic type does not match.
✗ Incorrect
x holds a float64, but we assert int, so v is zero value 0 and ok is false.
🔧 Debug
advanced2:00remaining
Identify the runtime error with empty interface
What runtime error will this Go program produce?
Go
package main import "fmt" func main() { var x interface{} = "golang" v := x.(int) fmt.Println(v) }
Attempts:
2 left
💡 Hint
Direct type assertion without checking can cause panic if types mismatch.
✗ Incorrect
The program panics because x holds a string but is asserted as int without checking.
🚀 Application
expert1:30remaining
Number of elements in slice of empty interfaces
What is the length of the slice 'data' after this code runs?
Go
package main func main() { data := []interface{}{1, "two", 3.0, true} // What is len(data)? }
Attempts:
2 left
💡 Hint
Count the number of elements inside the slice literal.
✗ Incorrect
The slice contains four elements of different types, all stored as interface{} values.