Challenge - 5 Problems
Slice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Remember len returns the number of elements, cap returns the capacity.
✗ Incorrect
The slice s is created with length 3 and capacity 5, so len(s) is 3 and cap(s) is 5.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Slice from index 1 up to but not including 4.
✗ Incorrect
Slicing arr[1:4] includes elements at indices 1, 2, and 3: 20, 30, 40.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Slices share the underlying array with the original array.
✗ Incorrect
Modifying s[1] changes arr[1] because s references the same array.
❓ Predict Output
advanced2: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)) }
Attempts:
2 left
💡 Hint
Capacity is calculated as max - start index in full slice expression.
✗ Incorrect
Length is 3-1=2, capacity is 4-1=3.
❓ Predict Output
expert3: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) }
Attempts:
2 left
💡 Hint
Appending may create a new underlying array if capacity is exceeded.
✗ Incorrect
Appending 4 to s creates a new array for s, so modifying arr[0] affects arr but not s's first element.