A slice in Go is a flexible, dynamically-sized view into the elements of an array. It provides access to a segment of an array and can grow or shrink as needed.
Click to reveal answer
beginner
How do you append an element to a slice?
Use the built-in append function. For example: slice = append(slice, element) adds element to the end of slice.
Click to reveal answer
intermediate
How can you remove an element at index i from a slice?
You can remove an element by slicing and combining parts: slice = append(slice[:i], slice[i+1:]...). This skips the element at index i.
Click to reveal answer
beginner
What does slicing a slice like slice[a:b] do?
It creates a new slice referencing elements from index a up to but not including b. The original slice is not copied, just referenced.
Click to reveal answer
intermediate
How do you copy elements from one slice to another?
Use the built-in copy function: copy(dest, src) copies elements from src to dest. It copies up to the smaller length of the two slices.
Click to reveal answer
What happens when you append an element to a slice that has no extra capacity?
AA new underlying array is allocated with more capacity, and the slice points to it.
BThe element is discarded because the slice is full.
CThe program crashes with an error.
DThe element replaces the last element in the slice.
✗ Incorrect
When appending to a slice with no extra capacity, Go allocates a new array with more space and copies existing elements before adding the new one.
How do you get a slice of the first 3 elements of a slice named data?
Adata[0:3]
Bdata[3:0]
CBoth A and D
Ddata[:3]
✗ Incorrect
Both data[0:3] and data[:3] return a slice of the first 3 elements.
Which function is used to copy elements from one slice to another?
Aappend
Bcopy
Cslice
Dmove
✗ Incorrect
The built-in copy function copies elements from a source slice to a destination slice.
How do you remove the element at index 2 from a slice named items?
Aitems = append(items[3:], items[:2]...)
Bitems = items[2:]
Citems = items[:2]
Ditems = append(items[:2], items[3:]...)
✗ Incorrect
Appending items[:2] and items[3:] skips the element at index 2, effectively removing it.
What is the length of a newly created slice with make([]int, 5, 10)?
A5
B10
C0
D15
✗ Incorrect
The length is 5, which is the number of initialized elements. The capacity is 10, the total space allocated.
Explain how to add and remove elements from a slice in Go.
Think about how you can combine parts of the slice to exclude an element.
You got /3 concepts.
Describe what happens internally when you append an element to a slice that is already full.
Consider how Go manages memory for slices when they grow.
You got /4 concepts.
Practice
(1/5)
1. What does the slice expression mySlice[2:5] do in Go?
easy
A. It returns a new slice containing elements from index 2 up to, but not including, index 5.
B. It returns elements from index 2 to index 5, including index 5.
C. It removes elements from index 2 to 5 from the original slice.
D. It appends elements from index 2 to 5 to the original slice.
Solution
Step 1: Understand slice syntax
The slice expression mySlice[2:5] extracts elements starting at index 2 up to, but not including, index 5. Go slices use half-open intervals, so the element at index 5 is not included in the new slice.
Final Answer:
It returns a new slice containing elements from index 2 up to, but not including, index 5. -> Option A
Quick Check:
Slice[start:end] excludes end index [OK]
Hint: Remember slice end index is excluded in Go slices [OK]
Common Mistakes:
Including the end index element in the slice
Thinking slice removes elements from original
Confusing append with slicing
2. Which of the following is the correct way to append an element 5 to a slice nums in Go?
easy
A. append(nums, 5)
B. nums += 5
C. nums.append(5)
D. nums = append(nums, 5)
Solution
Step 1: Recall append function usage
In Go, append returns a new slice with the element added, so you must assign it back. nums = append(nums, 5) correctly assigns the result back to nums. append(nums, 5) ignores the returned slice. nums.append(5) uses invalid method syntax. nums += 5 is invalid for slices.
Final Answer:
nums = append(nums, 5) -> Option D
Quick Check:
append returns new slice, assign it [OK]
Hint: Always assign append result back to slice variable [OK]
The slice slice := nums[1:4] references the original array elements at indexes 1, 2, and 3. Changing slice[0] to 10 changes nums[1] because slices share the underlying array. Output: [1 10 3 4 5]
Final Answer:
[1 10 3 4 5] -> Option A
Quick Check:
Slices share array, modifying slice changes original [OK]
Hint: Changing slice elements changes original array too [OK]
A. Cannot print a slice directly with fmt.Println.
B. Syntax error in slice declaration.
C. Slicing beyond the slice length causes a runtime panic.
D. Missing import for fmt package.
Solution
Step 1: Check slice bounds
The original slice nums has length 3, so valid indices are 0 to 2. Attempting nums[1:4] tries to access index 3 which is out of range, causing a runtime panic.
Final Answer:
Slicing beyond the slice length causes a runtime panic. -> Option C
Quick Check:
Slice end index must be ≤ length [OK]
Hint: Slice end index cannot exceed slice length [OK]
Common Mistakes:
Assuming slice auto-expands
Confusing compile error with runtime panic
Ignoring slice length when slicing
5. Given a slice data := []int{2, 4, 6, 8, 10, 12}, how can you create a new slice containing only the even numbers greater than 6 using slicing and append?
hard
A. newSlice := data[2:5]
B. newSlice := append([]int{}, data[3:]...)
C. newSlice := append(data[:3], data[4:]...)
D. newSlice := data[3:len(data)]
Solution
Step 1: Identify elements > 6
Elements greater than 6 are 8, 10, 12 at indexes 3, 4, 5. Using data[3:] gets these elements. To create a new slice (not referencing original), append to empty slice: newSlice := append([]int{}, data[3:]...).
Final Answer:
newSlice := append([]int{}, data[3:]...) -> Option B
Quick Check:
Slice and append to copy subset [OK]
Hint: Use append to copy slice subset safely [OK]
Common Mistakes:
Using slice without append copies reference
Incorrect slice indexes for desired elements
Trying to remove elements by slicing incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.