0
0
Goprogramming~20 mins

Slicing operations in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Slice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of slice length and capacity
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := make([]int, 3, 5)
    fmt.Println(len(s), cap(s))
}
A5 3
B3 5
C0 3
D3 3
Attempts:
2 left
💡 Hint
Remember len returns the number of elements, cap returns the capacity.
Predict Output
intermediate
2:00remaining
Result of slicing a slice
What will be printed by this Go program?
Go
package main
import "fmt"
func main() {
    arr := []int{10, 20, 30, 40, 50}
    s := arr[1:4]
    fmt.Println(s)
}
A[30 40 50]
B[10 20 30]
C[20 30 40 50]
D[20 30 40]
Attempts:
2 left
💡 Hint
Slice from index 1 up to but not including 4.
Predict Output
advanced
2:00remaining
Effect of modifying a slice on the underlying array
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    arr := [3]int{1, 2, 3}
    s := arr[:]
    s[1] = 20
    fmt.Println(arr)
}
A[1 20 3]
B[1 2 3]
C[20 2 3]
D[1 20 20]
Attempts:
2 left
💡 Hint
Slices share the underlying array with the original array.
Predict Output
advanced
2:00remaining
Capacity after slicing with full slice expression
What will this program print?
Go
package main
import "fmt"
func main() {
    arr := []int{1, 2, 3, 4, 5}
    s := arr[1:3:4]
    fmt.Println(len(s), cap(s))
}
A2 4
B3 4
C2 3
D3 3
Attempts:
2 left
💡 Hint
Capacity is calculated as max - start index in full slice expression.
Predict Output
expert
3:00remaining
Output after appending to a slice and modifying original array
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    arr := []int{1, 2, 3}
    s := arr[0:2:2]
    s = append(s, 4)
    arr[0] = 10
    fmt.Println(s, arr)
}
A[1 2 4] [10 2 3]
B[10 2 4] [10 2 3]
C[1 2 4] [1 2 3]
D[10 2 3] [10 2 3]
Attempts:
2 left
💡 Hint
Appending may create a new underlying array if capacity is exceeded.