Golang Arrays & Slices Quiz
40 comprehensive questions on Golang arrays and slices, covering fixed-size arrays, slice creation, capacity & length, internals, and copying operations — with 18 code examples demonstrating array and slice behavior.
Question 1
What is the main difference between arrays and slices in Golang?
Question 2
How do you declare a fixed-size array in Golang?
Question 3
What happens when you try to append to a fixed-size array?
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
arr = append(arr, 4) // This line
fmt.Println(arr)
}Question 4
How do you create a slice from an array?
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
s := arr[1:4]
fmt.Println(s) // What is printed?
}Question 5
What does the make() function do for slices?
Question 6
What is the difference between length and capacity of a slice?
Question 7
What will be the length and capacity after this operation?
package main
import "fmt"
func main() {
s := make([]int, 3, 5)
fmt.Println(len(s), cap(s)) // 3 5
s = s[1:]
fmt.Println(len(s), cap(s))
}Question 8
What is a backing array in slice terminology?
Question 9
What happens when you modify a slice element?
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
s1 := arr[:]
s2 := arr[:]
s1[0] = 99
fmt.Println(arr[0], s2[0])
}Question 10
How does append() work when capacity is exceeded?
Question 11
What is the result of this copy operation?
package main
import "fmt"
func main() {
s1 := []int{1, 2, 3}
s2 := make([]int, 2)
copy(s2, s1)
fmt.Println(s2)
}Question 12
In a scenario where you're building a buffer that frequently grows, which slice creation method should you prefer?
Question 13
What does arr[:] create when arr is an array?
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
s := arr[:]
fmt.Printf("Type: %T, Value: %v\n", s, s)
}Question 14
Why might you use arrays instead of slices?
Question 15
What happens to capacity when you slice a slice?
package main
import "fmt"
func main() {
s := make([]int, 3, 10)
s2 := s[1:3]
fmt.Println(cap(s2))
}Question 16
In a high-performance application processing large datasets, you're implementing a ring buffer using slices. What capacity management strategy should you use?
Question 17
What is the zero value of a slice?
Question 18
How do you check if a slice is nil?
package main
import "fmt"
func main() {
var s []int
if s == nil {
fmt.Println("nil slice")
}
}Question 19
What does this slicing operation do?
package main
import "fmt"
func main() {
s := []int{1, 2, 3, 4, 5}
s = s[:2]
fmt.Println(s)
}Question 20
In a web server handling HTTP requests, you're collecting request data in a slice. As requests come in rapidly, what happens if you don't manage capacity properly?
Question 21
What is the difference between these slice declarations?
var s1 []int
s2 := []int{}
var s3 = make([]int, 0)Question 22
How does the copy() function handle overlapping slices?
package main
import "fmt"
func main() {
s := []int{1, 2, 3, 4, 5}
copy(s[2:], s[:3])
fmt.Println(s)
}Question 23
What happens when you pass a slice to a function?
package main
import "fmt"
func modify(s []int) {
s[0] = 99
}
func main() {
s := []int{1, 2, 3}
modify(s)
fmt.Println(s[0])
}Question 24
In a data processing pipeline, you're implementing a filter function that removes elements from a slice. Which approach should you use to avoid modifying the original slice?
Question 25
What is the capacity after this append operation?
package main
import "fmt"
func main() {
s := make([]int, 3, 5)
s = append(s, 1, 2, 3)
fmt.Println(cap(s))
}Question 26
Why are slices more common than arrays in Golang code?
Question 27
What does this code demonstrate about slice internals?
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
s1 := arr[1:4]
s2 := arr[2:5]
s1[1] = 99
fmt.Println(arr, s2)
}Question 28
In a memory-constrained embedded system, you're working with sensor data. Would you prefer arrays or slices for fixed-size sensor buffers?
Question 29
What is the result of this append beyond capacity?
package main
import "fmt"
func main() {
s := make([]int, 3, 3)
s = append(s, 4)
fmt.Println(len(s), cap(s))
}Question 30
How do you create a slice with specific initial values?
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
fmt.Println(s)
}Question 31
In a concurrent program with multiple goroutines appending to a shared slice, what synchronization is needed?
Question 32
What happens when you slice beyond the backing array?
package main
func main() {
s := []int{1, 2, 3}
s2 := s[1:10] // len=3, cap=3
}Question 33
Why is understanding capacity important for performance?
Question 34
What does this code illustrate about slice copying?
package main
import "fmt"
func main() {
s1 := []int{1, 2, 3}
s2 := s1 // assignment
s2[0] = 99
fmt.Println(s1[0]) // 99
}Question 35
In a database query result processing scenario, you're receiving rows as slices. If you need to modify results without affecting the original data, what should you do?
Question 36
What is the relationship between arrays and slices in terms of memory layout?
Question 37
How do you efficiently remove an element from the middle of a slice?
package main
import "fmt"
func main() {
s := []int{1, 2, 3, 4, 5}
i := 2 // remove index 2
s = append(s[:i], s[i+1:]...)
fmt.Println(s)
}Question 38
What happens to the backing array when a slice is garbage collected?
Question 39
In a real-time system with strict latency requirements, why should you avoid frequent slice growth?
Question 40
What is the most important principle to remember when working with slices and their backing arrays?
