0
0
Goprogramming~5 mins

Slice and array relationship in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a slice in Go?
A slice in Go is a flexible, dynamic view into the elements of an array. It does not store data itself but references an underlying array.
Click to reveal answer
beginner
How does a slice relate to its underlying array?
A slice points to a segment of an array. Changes to the slice elements affect the underlying array and vice versa.
Click to reveal answer
intermediate
What happens when you append to a slice that has no extra capacity?
Go creates a new underlying array with more capacity, copies the old elements, and appends the new ones. The slice then points to this new array.
Click to reveal answer
beginner
How can you create a slice from an array?
You can create a slice by specifying a range of indices on an array, like: slice := array[start:end].
Click to reveal answer
beginner
Does modifying a slice affect the original array?
Yes, because the slice references the original array's elements, modifying the slice changes the array's data.
Click to reveal answer
What does a Go slice store internally?
AOnly the length of the array
BA copy of the array elements
CA pointer to an array, length, and capacity
DThe entire array data
If you change an element in a slice, what happens to the underlying array?
AThe array element changes too
BThe array remains unchanged
CThe array is copied and changed separately
DThe slice breaks the link to the array
What happens when you append to a slice that has enough capacity?
AThe program crashes
BA new array is created
CThe slice length stays the same
DThe element is added to the existing array
How do you create a slice from an array named 'arr' from index 2 to 5?
Aslice := arr[3:5]
Bslice := arr[2:5]
Cslice := arr[2:6]
Dslice := arr[2..5]
Which of these is true about slices in Go?
ASlices can grow beyond the size of the underlying array
BSlices always copy the array data
CSlices cannot be created from arrays
DSlices have fixed length and capacity
Explain how a slice and its underlying array are connected in Go.
Think about how slices are views into arrays.
You got /4 concepts.
    Describe what happens internally when you append an element to a slice that has no remaining capacity.
    Consider slice capacity and memory allocation.
    You got /4 concepts.