restructure: arrays and slices
This commit is contained in:
123
16-slices/questions/1-slices-vs-arrays.md
Normal file
123
16-slices/questions/1-slices-vs-arrays.md
Normal file
@ -0,0 +1,123 @@
|
||||
# Slices vs Arrays Quiz
|
||||
|
||||
## Why you want to use a slice instead of an array?
|
||||
1. I like arrays more
|
||||
2. I want to create a dynamic collection, so I need an array
|
||||
3. A slice's length is dynamic, so I can create dynamic collections *CORRECT*
|
||||
|
||||
|
||||
## Where does the length of a slice belong to?
|
||||
1. Compile-Time
|
||||
2. Runtime *CORRECT*
|
||||
3. Walk-Time
|
||||
4. Sleep-Time
|
||||
|
||||
> **2:** A slice's length is not a part of its type. So its length can change at runtime.
|
||||
|
||||
|
||||
## Which function call below is correct?
|
||||
```go
|
||||
// Let's say there's a function like this.
|
||||
func sort(nums []int) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
1. sort([...]int{3, 1, 6})
|
||||
2. sort([]int32{3, 1, 6})
|
||||
3. sort([]int{3, 1, 6}) *CORRECT*
|
||||
|
||||
> **1:** You can't call the sort function using an array. It expects an int slice.
|
||||
>
|
||||
> **2:** You can't call the sort function using an int32 slice. It expects an int slice.
|
||||
>
|
||||
> **3:** That's right! You can pass an int slice to the sort function.
|
||||
|
||||
|
||||
## What is the zero value of this slice?
|
||||
```go
|
||||
var tasks []string
|
||||
```
|
||||
1. 0
|
||||
2. 1
|
||||
3. nil *CORRECT*
|
||||
4. unknown
|
||||
|
||||
> **3:** This is a nil slice. Unlike an array, a slice's zero value is nil.
|
||||
|
||||
|
||||
## What does this code print?
|
||||
```go
|
||||
var tasks []string
|
||||
fmt.Println(len(tasks))
|
||||
```
|
||||
|
||||
1. 0 *CORRECT*
|
||||
2. 1
|
||||
3. nil
|
||||
4. It doesn't work.
|
||||
|
||||
> **1:** Yes, you can use the len function on a nil slice. It returns 0 because the slice doesn't contain any elements yet.
|
||||
|
||||
|
||||
## What does this code print?
|
||||
```go
|
||||
var tasks []string
|
||||
fmt.Println(tasks[0])
|
||||
```
|
||||
|
||||
1. 0
|
||||
2. 1
|
||||
3. nil
|
||||
4. It doesn't work. *CORRECT*
|
||||
|
||||
> **4:** You can't get an element that does not exist. A nil slice does not contain any elements.
|
||||
|
||||
|
||||
## Which declaration below is a correct slice declaration?
|
||||
1. [...]int{}
|
||||
2. [2]string{"hello", "world"}
|
||||
3. []string{"hello", "world"} *CORRECT*
|
||||
4. string[2]{"hello", world"}
|
||||
|
||||
|
||||
## This code doesn't work, why?
|
||||
```go
|
||||
colors := []string{"red", "blue", "green"}
|
||||
tones := []string{"dark", "light"}
|
||||
|
||||
if colors == tones {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
1. The slices have different lengths
|
||||
2. If statement doesn't contain any statements
|
||||
3. Slices cannot be compared *CORRECT*
|
||||
|
||||
> **3:** That's right! A slice value can only be compared to a nil value.
|
||||
|
||||
|
||||
## What is the length of this slice?
|
||||
```go
|
||||
[]uint64{}
|
||||
```
|
||||
|
||||
1. 64
|
||||
2. 1
|
||||
3. 0 *CORRECT*
|
||||
4. Error
|
||||
|
||||
> **3:** That's right. This is an empty slice, it doesn't contain any elements.
|
||||
|
||||
|
||||
## What is the length of this slice?
|
||||
```go
|
||||
[]string{"i'm", "going", "to", "stay", "\"here\""}
|
||||
```
|
||||
|
||||
1. 0
|
||||
2. 1
|
||||
3. 2
|
||||
4. 3
|
||||
5. 4
|
||||
6. 5 *CORRECT*
|
66
16-slices/questions/2-appending.md
Normal file
66
16-slices/questions/2-appending.md
Normal file
@ -0,0 +1,66 @@
|
||||
# Appending Quiz
|
||||
|
||||
## How does the append function work?
|
||||
1. It appends new elements to the given slice on the fly and returns a new slice, normally, it doesn't change the given slice *CORRECT*
|
||||
2. It appends new elements to the given slice and returns the existing slice
|
||||
3. It appends new elements to the given slice and returns a new slice, it also changes the given slice
|
||||
|
||||
> **1:** Yes, the append function doesn't change the given slice unless you overwrite the result of the append function back to the original slice. Most of the times, this is true.
|
||||
>
|
||||
> **2:** It doesn't return the existing slice, it returns a new slice. That's why you usually save the result of the append back to the original slice.
|
||||
>
|
||||
> **3:** It doesn't change the given slice, it creates and returns a new slice. Most of the times, this is true.
|
||||
|
||||
|
||||
## When you call the append function, where does it append the new elements?
|
||||
1. It appends them at the beginning of the given slice
|
||||
2. It appends them at the middle of the given slice
|
||||
3. It appends them after the length of the given slice *CORRECT*
|
||||
|
||||
> **3:** Yes! The append function appends the new elements by looking at the length of the given slice and then returns a new slice with the newly appended elements.
|
||||
|
||||
|
||||
## What's the problem with the following code?
|
||||
```go
|
||||
nums := []int{9, 7, 5}
|
||||
append(nums, []int{2, 4, 6}...)
|
||||
|
||||
fmt.Println(nums[3])
|
||||
```
|
||||
1. It can't append to an int slice
|
||||
2. It can't append a slice to another slice
|
||||
3. It tries to get an element that doesn't exist yet *CORRECT*
|
||||
|
||||
> **3:** That's right. The append function returns a new slice with the newly added elements. But here, the code doesn't save the result of the append, so the nums slice only has 3 elements. So, `nums[3]` is invalid, because there are only 3 elements in the nums slice.
|
||||
|
||||
|
||||
## What does this program print?
|
||||
```go
|
||||
nums := []int{9, 7, 5}
|
||||
evens := append(nums, []int{2, 4, 6}...)
|
||||
|
||||
fmt.Println(nums, evens)
|
||||
```
|
||||
1. [9 7 5] [2 4 6]
|
||||
2. [9 7 5] [9 7 5 2 4 6] *CORRECT*
|
||||
3. [9 7 5 2 4 6] [2 4 6]
|
||||
4. [9 7 5 2 4 6] [9 7 5 2 4 6]
|
||||
|
||||
> **2:** It appends the new elements to the nums slice but it saves the returned slice to the evens slice. So, the nums slice doesn't change. That's why, it prints the original elements from the nums slice first, then it prints the evens slice with the newly added elements.
|
||||
>
|
||||
> **3, 4:** It doesn't save the result of the append call back into the nums slice, so the nums slice doesn't contain the new elements.
|
||||
|
||||
|
||||
## What does this program print?
|
||||
```go
|
||||
nums := []int{9, 7, 5}
|
||||
nums = append(nums, 2, 4, 6)
|
||||
|
||||
fmt.Println(nums)
|
||||
```
|
||||
1. [9 7 5 2 4 6] *CORRECT*
|
||||
2. [9 7 5]
|
||||
3. [2 4 6]
|
||||
4. [2 4 6 9 7 5]
|
||||
|
||||
> **1:** It overwrites the nums slice with the new slice that is returned from the append function. So the nums slice has got the newly appended elements.
|
5
16-slices/questions/README.md
Normal file
5
16-slices/questions/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Slice Quizzes
|
||||
|
||||
* [Slices vs Arrays](1-slices-vs-arrays.md)
|
||||
|
||||
* [Appending](2-appending.md)
|
Reference in New Issue
Block a user