0
0
Goprogramming~20 mins

Empty interface in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Empty Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
A
interface {} 42
interface {} hello
BCompilation error
C
42
hello
D
int 42
string hello
Attempts:
2 left
💡 Hint
Empty interface can hold any type, and %T prints the dynamic type.
🧠 Conceptual
intermediate
1:30remaining
Understanding empty interface usage
Which statement about the empty interface in Go is TRUE?
AIt can hold values of any type.
BIt can only hold values of type interface{}.
CIt requires explicit type conversion to assign values.
DIt cannot be used as a function parameter.
Attempts:
2 left
💡 Hint
Think about what 'empty interface' means in Go.
Predict Output
advanced
2: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)
}
A3 true
B0 false
C3.14 true
DCompilation error
Attempts:
2 left
💡 Hint
Type assertion fails if the dynamic type does not match.
🔧 Debug
advanced
2: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)
}
Apanic: interface conversion: interface {} is string, not int
BCompilation error: cannot convert string to int
C0
Dgolang
Attempts:
2 left
💡 Hint
Direct type assertion without checking can cause panic if types mismatch.
🚀 Application
expert
1: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)?
}
A3
B1
C4
D0
Attempts:
2 left
💡 Hint
Count the number of elements inside the slice literal.