3.5 KiB
Slicing Quiz
What does this code print?
nums := []int{9, 7, 5}
nums = append(nums, 2, 4, 6)
fmt.Println(nums[2:4])
- [9 7 5 2 4 6]
- [5 2] CORRECT
- [4 6]
- [7 2]
- [9 7]
2: nums is [9 7 5 2 4 6]. So, nums[2:4] is [5 2]. Remember, in nums[2:4] -> 2 is the starting index, so nums[2] is 5; And 4 is the stopping position, so nums[4-1] is 2 (-1 because the stopping position is the element position). So, nums[2:4] returns a new slice that contains the elements at the middle of the nums slice.
What does this code print?
nums := []int{9, 7, 5}
nums = append(nums, 2, 4, 6)
fmt.Println(nums[:2])
- [9 7 5 2 4 6]
- [5 2]
- [4 6]
- [7 2]
- [9 7] CORRECT
5: nums is [9 7 5 2 4 6]. So, nums[:2] is nums[0:2] which in turn returns [9 7].
What does this code print?
nums := []int{9, 7, 5}
nums = append(nums, 2, 4, 6)
fmt.Println(nums[len(nums)-2:])
- [9 7 5 2 4 6]
- [5 2]
- [4 6] CORRECT
- [7 2]
- [9 7]
3: nums is [9 7 5 2 4 6]. So, nums[len(nums)-2:] is nums[4:6] (len(nums) is 6) which in turn returns [4 6].
What does this code print?
names := []string{"einstein", "rosen", "newton"}
names = names[:]
fmt.Println(names[:1])
- [einstein rosen newton]
- [einstein rosen]
- [einstein] CORRECT
- []
3: names[:] is names[0:3] -> [einstein rosen newton]. names[:1] is names[0:1] -> [einstein].
What is the type of the marked expression below?
names := []string{"einstein", "rosen", "newton"}
names[2:3] // <- marked
- []string CORRECT
- string
- names
- []int
1: Yes! A slicing expression returns a slice.
2: Remember, a slicing expression returns a slice. Did I give you the answer? Oops.
What is the type of the marked expression below?
names := []string{"einstein", "rosen", "newton"}
names[2] // <- marked
- []string
- string CORRECT
- names
- []int
1: Remember, an index expression returns an element value, not a slice.
2: Yep! An index expression returns an element value. The element type of the []string slice is string, so the returned value is a string value.
Which index expression returns the "rosen" element?
names := []string{"einstein", "rosen", "newton"}
names = names[1:len(names) - 1]
- names[0] CORRECT
- names[1]
- names[2]
1: That's right: names2 is ["rosen"] after the slicing.
2: That's not right. Remember, indexes are relative to a slice. names is ["einstein" "rosen" "newton"] but names[1:len(names)-1] is ["rosen"]. So, names2[1] is an error, it's because, the length of the last slice is 1.
What does this code print?
names := []string{"einstein", "rosen", "newton"}
names = names[1:]
names = names[1:]
fmt.Println(names)
- [einstein rosen newton]
- [rosen newton]
- [newton] CORRECT
- []
3: Remember, slicing returns a new slice. Here, each
names = names[1:]
statement overwrites the names slice with the newly returned slice from the slicing. At first, the names was [einstein rosen newton]. After the first slicing, the names becomes [rosen newton]. After the second slicing, names becomes [newton]. See this for the complete explanation: https://play.golang.org/p/EsEHrSeByFR
What does this code print?
i := 2
s := fmt.Sprintf("i = %d * %d = %d", i, i, i*i)
fmt.Print(s)
- i = i * i = i*i
- i = %d * %d = %d
- i = 2 * 2 = 2
- i = 2 * 2 = 4 CORRECT
4: Awesome! Sprintf works just like Printf. Instead of printing the result to standard out (usually to command-line prompt), it returns a string value.