A slice in Go is a flexible, dynamically-sized view into the elements of an array. It lets you work with parts of an array without copying data.
Click to reveal answer
beginner
How do you create a slice from an array or another slice?
You use the slicing syntax: slice := array[start:end]. This creates a new slice from index start up to but not including end.
Click to reveal answer
beginner
What happens if you omit the start or end index in a slice operation?
If you omit start, it defaults to 0 (start of the array). If you omit end, it defaults to the length of the array or slice.
Click to reveal answer
intermediate
What is the difference between length and capacity of a slice?
Length is how many elements the slice currently holds. Capacity is how many elements the slice can hold before it needs to grow (based on the underlying array).
Click to reveal answer
intermediate
How can slicing affect the original array?
Slices share the same underlying array. Changing an element in a slice changes the same element in the original array and other slices sharing it.
Click to reveal answer
What does arr[2:5] return if arr is a slice?
AElements from index 3 to 5
BElements from index 2 up to and including 5
CElements from index 0 to 5
DElements from index 2 up to but not including 5
✗ Incorrect
Slicing in Go includes the start index but excludes the end index.
What is the length of the slice created by slice := arr[:]?
ALength of arr
B0
CCapacity of arr
DUndefined
✗ Incorrect
Omitting start and end means the slice includes the whole array, so length equals the array's length.
If you change an element in a slice, what happens to the original array?
AThe original array changes at that element
BThe original array stays the same
CA new array is created
DThe slice becomes invalid
✗ Incorrect
Slices share the underlying array, so changes affect the original array.
What does the capacity of a slice represent?
ANumber of slices created from the array
BCurrent number of elements in the slice
CMaximum number of elements the slice can hold before resizing
DSize of the underlying array
✗ Incorrect
Capacity is how many elements the slice can hold before it needs to allocate a new underlying array.
Which of these is a valid way to create a slice from an array arr?
Aslice := arr(1,4)
Bslice := arr[1:4]
Cslice := arr[1..4]
Dslice := arr.slice(1,4)
✗ Incorrect
Go uses the syntax arr[start:end] for slicing.
Explain how slicing works in Go and how it relates to the underlying array.
Think about how slices are views into arrays, not copies.
You got /4 concepts.
Describe the difference between the length and capacity of a slice and why it matters.
Consider what happens when you append elements to a slice.
You got /4 concepts.
Practice
(1/5)
1. In Go, what does the slice expression arr[2:5] do?
easy
A. Extracts elements from index 2 up to and including index 5
B. Extracts elements from index 2 up to but not including index 5
C. Extracts elements from index 3 to 5
D. Extracts elements from index 0 to 5
Solution
Step 1: Understand slice indexing in Go
Go slices include the start index but exclude the end index.
Step 2: Apply to arr[2:5]
This means elements at indexes 2, 3, and 4 are included, but not index 5.
Final Answer:
Extracts elements from index 2 up to but not including index 5 -> Option B
Quick Check:
Slice = arr[2:5] includes indexes 2,3,4 [OK]
Hint: Remember: start included, end excluded in slices [OK]
Common Mistakes:
Including the end index element
Confusing start and end indexes
Thinking slice includes elements before start
2. Which of the following is the correct syntax to slice a Go slice arr from the beginning up to index 4 (excluding 4), omitting the start index?
easy
A. arr[:4]
B. arr[0:4]
C. arr[1:4]
D. arr[4:]
Solution
Step 1: Recall slice syntax for start omitted
Omitting the start index means slicing from the beginning (index 0).
Step 2: Check the options
arr[:4] omits the start index (defaults to 0), slicing up to but excluding index 4. arr[0:4] explicitly includes the start index.
Final Answer:
arr[:4] -> Option A
Quick Check:
Start omitted means from 0 [OK]
Hint: Omit start to slice from beginning: arr[:end] [OK]