Challenge - 5 Problems
Slice Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember that append adds elements to the end of the slice.
✗ Incorrect
The append function adds the elements 4 and 5 to the existing slice s, resulting in [1 2 3 4 5].
❓ Predict Output
intermediate2: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]) }
Attempts:
2 left
💡 Hint
Remember slice[start:end] includes start but excludes end.
✗ Incorrect
The slice s[1:4] includes elements at indices 1, 2, and 3, which are "b", "c", and "d".
❓ Predict Output
advanced2: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)) }
Attempts:
2 left
💡 Hint
Length is the number of elements, capacity is the underlying array size.
✗ Incorrect
Initially, length is 3 and capacity is 5. Extending the slice to length 5 is valid and updates length to 5 while capacity stays 5.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Appending slices can be used to remove elements.
✗ Incorrect
The element at index 2 (value 30) is removed by appending the parts before and after it.
🧠 Conceptual
expert2: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] }
Attempts:
2 left
💡 Hint
You cannot extend a slice beyond its capacity.
✗ Incorrect
Trying to slice beyond the capacity causes a runtime panic with 'slice bounds out of range'.