0
0
Goprogramming~20 mins

Common slice operations in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Slice Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this slice append operation?
Consider the following Go code snippet. What will be printed?
Go
package main
import "fmt"
func main() {
    s := []int{1, 2, 3}
    s = append(s, 4, 5)
    fmt.Println(s)
}
A[4 5]
B[1 2 3]
C[1 2 3 4 5]
DCompilation error
Attempts:
2 left
💡 Hint
Remember that append adds elements to the end of the slice.
Predict Output
intermediate
2:00remaining
What does this slice slicing produce?
What will be the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := []string{"a", "b", "c", "d", "e"}
    fmt.Println(s[1:4])
}
Aruntime error: index out of range
B["a" "b" "c"]
C["c" "d" "e"]
D["b" "c" "d"]
Attempts:
2 left
💡 Hint
Remember slice[start:end] includes start but excludes end.
Predict Output
advanced
2:00remaining
What is the output of this slice capacity and length code?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    s := make([]int, 3, 5)
    fmt.Println(len(s), cap(s))
    s = s[:5]
    fmt.Println(len(s), cap(s))
}
A
3 5
3 5
B
3 5
5 5
C
3 3
5 5
Druntime error: slice bounds out of range
Attempts:
2 left
💡 Hint
Length is the number of elements, capacity is the underlying array size.
Predict Output
advanced
2:00remaining
What happens when you remove an element from a slice?
What will be printed by this Go program?
Go
package main
import "fmt"
func main() {
    s := []int{10, 20, 30, 40, 50}
    i := 2
    s = append(s[:i], s[i+1:]...)
    fmt.Println(s)
}
A[10 20 40 50]
Bruntime error: slice bounds out of range
C[10 20 30 50]
D[10 20 30 40 50]
Attempts:
2 left
💡 Hint
Appending slices can be used to remove elements.
🧠 Conceptual
expert
2:00remaining
What error does this slice operation cause?
What error will this Go code produce when run?
Go
package main
func main() {
    s := []int{1, 2, 3}
    s = s[:4]
}
Aruntime error: slice bounds out of range
Bcompilation error: invalid slice index
Cno error, slice extended with zero values
Dpanic: index out of range
Attempts:
2 left
💡 Hint
You cannot extend a slice beyond its capacity.