3.8 KiB
Arrays Quiz
What's the length of this array literal?
gadgets := [...]string{"Mighty Mouse", "Amazing Keyboard", "Shiny Monitor"}
- 0
- 1
- 2
- 3 CORRECT
4: Yes! There are 3 elements in the element list. So, Go sets the length of the array to 3.
What's the type and length of this array literal?
gadgets := [...]string{}
- [0]string and 0 CORRECT
- [0]string{} and 0
- [1]string and 1
- [1]string{} and 1
1: Yes! There are no elements in the element list. So, Go sets the length of the array to 0.
What does this program print?
package main
import "fmt"
func main() {
gadgets := [3]string{"Confused Drone"}
fmt.Printf("%q\n", gadgets)
}
- [3]string{"Confused Drone", "", ""}
- [1]string{"Confused Drone"}
- ["Confused Drone" "" ""] CORRECT
- ["Confused Drone"]
1: %q verb doesn't print the type of an array.
2, 4: Array's length cannot change depending on the elements.
3: Yes! Go sets the uninitialized elements to their zero values.
Are these arrays comparable?
gadgets := [3]string{"Confused Drone"}
gears := [...]string{"Confused Drone"}
fmt.Println(gadgets == gears)
- Yes, because they have identical types and elements
- No, because their types are different CORRECT
- No, because their elements are different
2: Yes! gadget's type is [3]string whereas gears's type is [1]string.
What does this program print?
gadgets := [3]string{"Confused Drone", "Broken Phone"}
gears := gadgets
gears[2] = "Shiny Mouse"
fmt.Printf("%q\n", gadgets)
- ["Confused Drone" "Broken Phone" "Shiny Mouse"]
- ["Confused Drone" "Broken Phone" ""] CORRECT
- ["" "" "Shiny Mouse"]
- ["" "" ""]
2: Yes! When you assign an array, Go creates a copy of the original array. So, gadgets and gears arrays are not connected. Changing one of them won't effect the other one.
What's the type of the digits array?
digits := [...][5]string{
{
"## ",
" # ",
" # ",
" # ",
"###",
},
[5]string{
"###",
" #",
"###",
" #",
"###",
},
}
- [...][5]string
- [2][2]string
- [2][5]string CORRECT
- [5][5]string
3: Awesome! There are two inner arrays, so the outer array's length becomes 2. Also note that,
[5]string
in front of the second element is unnecessary.
What does this program print?
rates := [...]float64{
5: 1.5,
2.5,
0: 0.5,
}
fmt.Printf("%#v\n", rates)
- [7]float64{0.5, 0, 0, 0, 0, 1.5, 2.5} CORRECT
- [7]float64{1.5, 2.5, 0.5, 0, 0, 0, 0}
- [3]float64{1.5, 2.5, 0.5}
- [3]float64{0.5, 2.5, 1.5}
1: That's right! For the explanation check out the example in the course repository here: https://github.com/inancgumus/learngo/tree/master/14-arrays/11-keyed-elements/06-keyed-and-unkeyed
Are these arrays equal?
type three [3]int
nums := [3]int{1, 2, 3}
nums2 := three{1, 2, 3}
fmt.Println(nums == nums2)
Note: To solve this question you need to watch the comparison and unnamed types lectures.
- Yes, because they have identical underlying types and elements CORRECT
- No, because their types are different
- No, because their length is different
1: Yes! They both have the same underlying types: [3]int
Are these array variables equal?
type (
threeA [3]int
threeB [3]int
)
nums := threeA{1, 2, 3}
nums2 := threeA(threeB{1, 2, 3})
fmt.Println(nums == nums2)
Note: To solve this question you need to the watch comparison and unnamed types lectures.
- Yes, because they have identical underlying types and elements CORRECT
- No, because their types are different
- No, because their length is different
1: Yes! Actually, arrays have different types, so normally they're not comparable. However, when you convert
threeB{1, 2, 3}
array tothreeA
type, they become comparable.