Golang Arrays & Slices Quiz

Golang
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is the main difference between arrays and slices in Golang?

A
Arrays have fixed size, slices are dynamic
B
Arrays are faster than slices
C
Slices cannot be indexed
D
Arrays are only for numbers
2

Question 2

How do you declare a fixed-size array in Golang?

A
var arr [5]int
B
var arr []int
C
var arr = []int{1,2,3}
D
var arr = make([]int, 5)
3

Question 3

What happens when you try to append to a fixed-size array?

go
package main

import "fmt"

func main() {
    arr := [3]int{1, 2, 3}
    arr = append(arr, 4)  // This line
    fmt.Println(arr)
}
A
Compilation error - cannot append to array
B
Array becomes [1,2,3,4]
C
Runtime panic
D
Array size increases
4

Question 4

How do you create a slice from an array?

go
package main

import "fmt"

func main() {
    arr := [5]int{1, 2, 3, 4, 5}
    s := arr[1:4]
    fmt.Println(s)  // What is printed?
}
A
[2 3 4]
B
[1 2 3 4 5]
C
[1 2 3]
D
Compilation error
5

Question 5

What does the make() function do for slices?

A
Allocates and initializes a slice with specified length and capacity
B
Creates an array
C
Copies a slice
D
Sorts a slice
6

Question 6

What is the difference between length and capacity of a slice?

A
Length is accessible elements, capacity is allocated space
B
They are always equal
C
Capacity is always smaller than length
D
Length includes nil elements
7

Question 7

What will be the length and capacity after this operation?

go
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))
}
A
2 4
B
2 5
C
3 5
D
1 4
8

Question 8

What is a backing array in slice terminology?

A
The underlying array that stores slice elements
B
A copy of the slice
C
The slice header
D
A pointer to the slice
9

Question 9

What happens when you modify a slice element?

go
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])
}
A
99 99 - all share backing array
B
99 1 - only s1 changes
C
1 1 - modification fails
D
Compilation error
10

Question 10

How does append() work when capacity is exceeded?

A
Allocates new backing array with doubled capacity
B
Fails with error
C
Truncates the slice
D
Only works within capacity
11

Question 11

What is the result of this copy operation?

go
package main

import "fmt"

func main() {
    s1 := []int{1, 2, 3}
    s2 := make([]int, 2)
    copy(s2, s1)
    fmt.Println(s2)
}
A
[1 2]
B
[1 2 3]
C
[0 0]
D
Runtime panic
12

Question 12

In a scenario where you're building a buffer that frequently grows, which slice creation method should you prefer?

A
make([]T, 0, initialCapacity) to pre-allocate capacity
B
[]T{} and let append handle growth
C
make([]T, largeSize) with zero length
D
Use arrays instead
13

Question 13

What does arr[:] create when arr is an array?

go
package main

import "fmt"

func main() {
    arr := [3]int{1, 2, 3}
    s := arr[:]
    fmt.Printf("Type: %T, Value: %v\n", s, s)
}
A
Type: []int, Value: [1 2 3]
B
Type: [3]int, Value: [1 2 3]
C
Compilation error
D
Runtime panic
14

Question 14

Why might you use arrays instead of slices?

A
When size is fixed and known at compile time, for type safety
B
When you need dynamic sizing
C
When memory efficiency is critical
D
Arrays are always better
15

Question 15

What happens to capacity when you slice a slice?

go
package main

import "fmt"

func main() {
    s := make([]int, 3, 10)
    s2 := s[1:3]
    fmt.Println(cap(s2))
}
A
9
B
2
C
10
D
3
16

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?

A
Pre-allocate maximum capacity with make() to avoid reallocations
B
Start with small capacity and let it grow
C
Use arrays for fixed-size buffers
D
Avoid slices entirely
17

Question 17

What is the zero value of a slice?

A
nil
B
Empty slice with length 0
C
Slice with nil elements
D
Compilation error
18

Question 18

How do you check if a slice is nil?

go
package main

import "fmt"

func main() {
    var s []int
    if s == nil {
        fmt.Println("nil slice")
    }
}
A
Use s == nil comparison
B
Check len(s) == 0
C
Check cap(s) == 0
D
Cannot check for nil
19

Question 19

What does this slicing operation do?

go
package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    s = s[:2]
    fmt.Println(s)
}
A
Truncates slice to first 2 elements
B
Creates new slice with first 2 elements
C
Removes first 2 elements
D
Runtime panic
20

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?

A
Frequent reallocations can cause GC pressure and latency spikes
B
Server crashes
C
Data gets corrupted
D
Nothing, append handles it automatically
21

Question 21

What is the difference between these slice declarations?

go
var s1 []int
s2 := []int{}
var s3 = make([]int, 0)
A
s1 is nil, s2 and s3 are empty but not nil
B
All are equivalent
C
s2 is nil, others are not
D
Compilation error
22

Question 22

How does the copy() function handle overlapping slices?

go
package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    copy(s[2:], s[:3])
    fmt.Println(s)
}
A
[1 2 1 2 3]
B
[1 2 3 4 5]
C
Runtime panic
D
Undefined behavior
23

Question 23

What happens when you pass a slice to a function?

go
package main

import "fmt"

func modify(s []int) {
    s[0] = 99
}

func main() {
    s := []int{1, 2, 3}
    modify(s)
    fmt.Println(s[0])
}
A
99 - slice is passed by reference
B
1 - slice is copied
C
Compilation error
D
Runtime panic
24

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?

A
Create a new slice and copy only desired elements
B
Modify the original slice in place
C
Use append to remove elements
D
Cannot avoid modifying original
25

Question 25

What is the capacity after this append operation?

go
package main

import "fmt"

func main() {
    s := make([]int, 3, 5)
    s = append(s, 1, 2, 3)
    fmt.Println(cap(s))
}
A
5
B
8 or more
C
6
D
3
26

Question 26

Why are slices more common than arrays in Golang code?

A
Slices are more flexible and safer for most use cases
B
Arrays are deprecated
C
Slices are faster
D
Arrays cannot be used in functions
27

Question 27

What does this code demonstrate about slice internals?

go
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)
}
A
Shared backing array - arr and s2 see change
B
Each slice has its own copy
C
Only s1 is modified
D
Runtime panic
28

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?

A
Arrays - no extra overhead from slice headers
B
Slices - more flexible for processing
C
Either, no difference
D
Neither, use maps
29

Question 29

What is the result of this append beyond capacity?

go
package main

import "fmt"

func main() {
    s := make([]int, 3, 3)
    s = append(s, 4)
    fmt.Println(len(s), cap(s))
}
A
4 6 (or more)
B
4 3
C
3 3
D
Runtime panic
30

Question 30

How do you create a slice with specific initial values?

go
package main

import "fmt"

func main() {
    s := []int{1, 2, 3}
    fmt.Println(s)
}
A
Composite literal []T{values}
B
make([]int, 3) then assign
C
Cannot initialize with values
D
Use new() function
31

Question 31

In a concurrent program with multiple goroutines appending to a shared slice, what synchronization is needed?

A
Mutex protection for all append operations
B
No synchronization needed, append is atomic
C
Use channels instead
D
Use arrays for thread safety
32

Question 32

What happens when you slice beyond the backing array?

go
package main

func main() {
    s := []int{1, 2, 3}
    s2 := s[1:10]  // len=3, cap=3
}
A
Runtime panic - slice bounds out of range
B
Slice extends with zeros
C
Compilation error
D
Creates new backing array
33

Question 33

Why is understanding capacity important for performance?

A
Prevents unnecessary allocations and copies during growth
B
Makes slices faster than arrays
C
Reduces memory usage
D
Not important for performance
34

Question 34

What does this code illustrate about slice copying?

go
package main

import "fmt"

func main() {
    s1 := []int{1, 2, 3}
    s2 := s1  // assignment
    s2[0] = 99
    fmt.Println(s1[0])  // 99
}
A
Slice assignment shares backing array
B
Creates independent copy
C
Only s2 is modified
D
Compilation error
35

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?

A
Make a copy of the slice using copy() or append
B
Modify directly, database won't care
C
Use arrays instead
D
Cannot modify database results
36

Question 36

What is the relationship between arrays and slices in terms of memory layout?

A
Slices reference contiguous memory segments, arrays own their memory
B
Both use the same memory layout
C
Arrays are stored on heap, slices on stack
D
No relationship
37

Question 37

How do you efficiently remove an element from the middle of a slice?

go
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)
}
A
[1 2 4 5] - element removed efficiently
B
[1 2 3 4 5] - no change
C
Runtime panic
D
Compilation error
38

Question 38

What happens to the backing array when a slice is garbage collected?

A
Backing array is collected if no other references exist
B
Backing array persists forever
C
Only the slice header is collected
D
GC doesn't affect backing arrays
39

Question 39

In a real-time system with strict latency requirements, why should you avoid frequent slice growth?

A
Reallocations can cause unpredictable pauses
B
Slices grow too slowly
C
Memory usage increases
D
Not a concern for real-time systems
40

Question 40

What is the most important principle to remember when working with slices and their backing arrays?

A
Slices share backing arrays - modifications are visible through all references
B
Slices are always independent copies
C
Backing arrays are automatically copied
D
No sharing occurs

QUIZZES IN Golang