Recall & Review
beginner
What is a slice in Go?
A slice is a flexible and dynamic view into the elements of an array. It allows you to work with sequences of elements without fixed size.
Click to reveal answer
beginner
How do you create a slice with predefined elements in Go?
You can create a slice by listing elements inside square brackets without specifying size, like:
slice := []int{1, 2, 3}.Click to reveal answer
intermediate
What does the built-in function
make do when creating a slice?make creates a slice with a specified length and capacity, for example: make([]int, 3, 5) creates a slice of length 3 and capacity 5.Click to reveal answer
intermediate
What is the difference between length and capacity of a slice?
Length is the number of elements the slice currently holds. Capacity is the total number of elements the slice can hold before needing to grow.
Click to reveal answer
beginner
How do you create an empty slice in Go?
You can create an empty slice by declaring it without elements:
var s []int or with make([]int, 0).Click to reveal answer
Which of the following creates a slice with 5 integers initialized to zero?
✗ Incorrect
make([]int, 5) creates a slice of length 5 with zero values. Option B creates a slice with one element '5'. Option C creates a slice of length 0 but capacity 5. Option D creates an array, not a slice.
What is the length of the slice created by
make([]string, 3, 10)?✗ Incorrect
The first number is the length, so the slice length is 3.
How do you declare a slice with elements 10, 20, and 30?
✗ Incorrect
Option B correctly creates a slice with those elements. Option C creates a slice of length 3 but with zero values. Option A creates an array, not a slice. Option D is invalid syntax.
What happens if you append an element to a slice that has reached its capacity?
✗ Incorrect
Go automatically allocates a new underlying array with more capacity and copies elements when needed.
Which of these is NOT a valid way to create an empty slice?
✗ Incorrect
Option D creates a slice of length 5, not empty. The others create empty slices.
Explain how to create a slice in Go with a specific length and capacity using the make function.
Think about the parameters make takes for slices.
You got /4 concepts.
Describe the difference between a slice's length and capacity and why it matters.
Consider what happens when you add elements beyond the current capacity.
You got /3 concepts.