Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a slice of integers.
Go
var numbers [1] Drag options to blanks, or click blank then click option'
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.
✗ Incorrect
In Go, slices are declared using [] followed by the type, like []int for a slice of integers.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
To create a slice from an array, use the slice expression with start and end indices like arr[1:4].
3fill in blank
hardFix 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'
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.
✗ Incorrect
The append function adds an integer value to the slice. The value must be an int, not a string.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of []int for the slice type.
Confusing length with capacity.
✗ Incorrect
The make function creates a slice of type []int with length 5.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong slice indices that cause out of range errors.
Not updating length correctly after append.
✗ Incorrect
The slice s is created from arr[1:3], then 5 is appended. The length is increased by 1 after append.