The length of a slice is the number of elements it currently holds. It tells you how many items you can access safely.
Click to reveal answer
beginner
What does the capacity of a slice represent in Go?
The capacity of a slice is the total number of elements the slice can hold in its underlying array before it needs to allocate more memory.
Click to reveal answer
beginner
How do you get the length and capacity of a slice named s in Go?
Use the built-in functions: len(s) gives the length, and cap(s) gives the capacity.
Click to reveal answer
intermediate
If you create a slice with make([]int, 3, 5), what are its length and capacity?
Length is 3 because you asked for 3 elements initially. Capacity is 5 because the underlying array can hold up to 5 elements before resizing.
Click to reveal answer
intermediate
What happens to the capacity of a slice when you append elements beyond its current capacity?
Go automatically creates a new underlying array with larger capacity, copies the existing elements, and appends the new ones. The capacity grows, usually doubling to reduce future allocations.
Click to reveal answer
What does len(s) return for a slice s in Go?
AThe size in bytes of the slice
BThe number of elements currently in the slice
CThe total capacity of the slice
DThe memory address of the slice
✗ Incorrect
len(s) returns how many elements are currently in the slice.
What does cap(s) return for a slice s in Go?
AThe total capacity of the slice's underlying array
BThe number of elements currently in the slice
CThe length of the slice plus one
DThe number of bytes used by the slice
✗ Incorrect
cap(s) returns the capacity, which is the size of the underlying array that the slice can use.
If you create a slice with make([]int, 4, 10), what is the length and capacity?
ALength 10, Capacity 4
BLength 10, Capacity 10
CLength 4, Capacity 4
DLength 4, Capacity 10
✗ Incorrect
The first number is length, the second is capacity.
What happens when you append an element to a slice that is already at full capacity?
AThe program crashes
BThe slice silently ignores the append
CGo allocates a new underlying array with larger capacity and copies elements
DThe slice length decreases
✗ Incorrect
Go automatically grows the slice's capacity by allocating a new array and copying elements.
Which built-in function do you use to find the capacity of a slice?
Acap()
Blen()
Csize()
Dcapacity()
✗ Incorrect
cap() returns the capacity of a slice.
Explain the difference between the length and capacity of a slice in Go.
Think about how many elements you can use now versus how many you can add before resizing.
You got /4 concepts.
Describe what happens internally when you append an element to a slice that has reached its capacity.
Imagine your backpack is full and you get a bigger one to carry more stuff.
You got /4 concepts.
Practice
(1/5)
1. In Go, what does the len() function return when used on a slice?
easy
A. The maximum number of elements the slice can hold
B. The total memory size of the slice in bytes
C. The index of the last element in the slice
D. The number of elements currently in the slice
Solution
Step 1: Understand the purpose of len() on slices
The len() function returns how many elements are currently stored in the slice.
Step 2: Differentiate from capacity
Capacity is about how many elements the slice can hold before resizing, not the current count.
Final Answer:
The number of elements currently in the slice -> Option D
Quick Check:
len(slice) = current element count [OK]
Hint: Remember: len = current items, cap = max before grow [OK]
Common Mistakes:
Confusing length with capacity
Thinking len returns memory size
Assuming len gives last index
2. Which of the following is the correct way to create a slice with length 3 and capacity 5 in Go?
easy
A. make([]int, 5, 3)
B. make([]int, 3, 5)
C. []int{3, 5}
D. make([]int, 3)
Solution
Step 1: Recall the make function signature for slices
make([]T, length, capacity) creates a slice with given length and capacity.
Step 2: Match length and capacity values
To get length 3 and capacity 5, use make([]int, 3, 5).
Final Answer:
make([]int, 3, 5) -> Option B
Quick Check:
make([]int, length, capacity) [OK]
Hint: make([]T, len, cap) sets length then capacity [OK]