0
0
Goprogramming~10 mins

Why slices are used in Go - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a slice of integers.

Go
var numbers [1]
Drag options to blanks, or click blank then click option'
Aint
B[5]int
Cmap[int]int
D[]int
Attempts:
3 left
💡 Hint
Common Mistakes
Using [5]int declares an array, not a slice.
Using int alone is just a single integer, not a slice.
2fill in blank
medium

Complete the code to create a slice from an array.

Go
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1]
Drag options to blanks, or click blank then click option'
A[1:4]
B[1]
C[:]
D[5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[1] returns a single element, not a slice.
Using arr[:] returns the whole array as a slice.
3fill in blank
hard

Fix the error in the code to append an element to a slice.

Go
var s []int
s = append(s, [1])
Drag options to blanks, or click blank then click option'
A5
B"5"
Cs
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an int causes a type error.
Trying to append the slice itself or the append function is incorrect.
4fill in blank
hard

Fill both blanks to create a slice with make and set its length.

Go
s := make([1], [2])
Drag options to blanks, or click blank then click option'
A[]int
B5
C10
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of []int for the slice type.
Confusing length with capacity.
5fill in blank
hard

Fill all three blanks to create a slice from an array and append a value.

Go
arr := [4]int{1, 2, 3, 4}
s := arr[1]
length := len(s) + [3]
s = append(s, [2])
Drag options to blanks, or click blank then click option'
A[1:3]
B5
C1
D[0:2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong slice indices that cause out of range errors.
Not updating length correctly after append.