4.6 KiB
Advanced Slice Operations Quiz
What are the length and capacity of the 'part' slice?
lyric := []string{"show", "me", "my", "silver", "lining"}
part := lyric[1:3:5]
- Length: 1 - Capacity: 5
- Length: 1 - Capacity: 3
- Length: 3 - Capacity: 5
- Length: 2 - Capacity: 4 CORRECT
4: General Formula:
[low:high:max]=>length = high - maxandcapacity = max - low.lyric[1:3]is["me" "my"].lyric[1:3:5]is["me" "my" "silver" "lining"]. So,[1:3]is the returned slice, length: 2.[1:3:5]limits the capacity to four because after the 1st element there are only four more elements.
What are the lengths and capacities of the slices below?
lyric := []string{"show", "me", "my", "silver", "lining"}
part := lyric[:2:2]
part = append(part, "right", "place")
- lyric's len: 5, cap: 5 — part's len: 5, cap: 5
- lyric's len: 3, cap: 1 — part's len: 2, cap: 3
- lyric's len: 5, cap: 5 — part's len: 4, cap: 4 CORRECT
- lyric's len: 3, cap: 1 — part's len: 2, cap: 3
3:
lyric[:2:2]= ["show" "me"]. After the append the part becomes: ["show" "me" "right" "place"] — so it allocates a new backing array.lyricstays the same:["show" "me" "my" "silver" "lining"].
When you might want to use the make function?
- To preallocate a backing array for a slice with a definite length CORRECT
- To create a slice faster
- To use less memory
1: Yes! You can use the make function to preallocate a backing array for a slice upfront.
What does the program print?
tasks := make([]string, 2)
tasks = append(tasks, "hello", "world")
fmt.Printf("%q\n", tasks)
- ["" "" "hello" "world"] CORRECT
- ["hello" "world"]
- ["hello" "world" "" ""]
1:
make([]string, 2)creates a slice with len: 2 and cap: 2, and it sets all the elements to their zero-values.append()appends after the length of the slice (after the first two elements). That's why the first two elements are zero-valued strings but the last two elements are the newly appended elements.
What does the program print?
tasks := make([]string, 0, 2)
tasks = append(tasks, "hello", "world")
fmt.Printf("%q\n", tasks)
- ["" "" "hello" "world"]
- ["hello" "world"] CORRECT
- ["hello" "world" "" ""]
2:
make([]string, 0, 2)creates a slice with len: 0 and cap: 2.append()appends after the length of the slice (at the beginning). That's why the first two elements are overwritten with the newly appended elements. This is a common usage pattern when you want to use themakeand theappendfunctions together.
What does the program print?
lyric := []string{"le", "vent", "nous", "portera"}
n := copy(lyric, make([]string, 4))
fmt.Printf("%d %q\n", n, lyric)
// -- USEFUL INFORMATION (but not required to solve the question) --
// In the following `copy` operation, `make` won't allocate
// a slice with a new backing array up to 32768 bytes
// (one string value is 8 bytes on a 64-bit machine).
//
// This is an optimization made by the Go compiler.
- 4 ["le" "vent" "le" "vent"]
- 4 ["le" "vent" "nous" "portera"]
- 4 ["" "" "" ""] CORRECT
- 0 []
3:
copycopies a newly created slice with four elements (make([]string, 4)) ontolyricslice. They both have 4 elements, so thecopycopies 4 elements. Remember:make()initializes a slice with zero-values of its element type. Here, this operation clears all the slice elements to their zero-values.
What does the program print?
spendings := [][]int{{200, 100}, {}, {50, 25, 75}, {500}}
total := spendings[2][1] + spendings[3][0] + spendings[0][0]
fmt.Printf("%d\n", total)
- 725 CORRECT
- 650
- 500
- 750
1:
spendings[2][1]= 25.spendings[3][0]= 500.spendings[0][0]= 200. 25 + 500 + 200 = 725
What does the program print?
spendings := [][]int{{1,2}}
// REMEMBER: %T prints the type of a given value
fmt.Printf("%T - ", spendings)
fmt.Printf("%T - ", spendings[0])
fmt.Printf("%T", spendings[0][0])
- [][]int{{1, 2}} - []int{1, 2} - int(2)
- [][]int - []int - int CORRECT
- []int - int - 2
- [][]int - [][]int - []int
2:
spendingsis a 2-dimensional int slice, so its type is [][]int. Its element type is:[]int, so:spendings[0]is[]int.spendings[0]'s element type is:int. Sospendings[0][0]'s type isint.
What is the 'element type' of the slice?
[][][3]int{{{10, 5, 9}}}
- [][][3]int
- [][]int
- [][3]int CORRECT
- [3]int
3:
[][][3]intis a multi-dimensional slice of[][3]intelements.[][3]intis a multi-dimensional slice of[3]intelements.[3]intis an array of 3intvalues.