0
0
Goprogramming~10 mins

Slice and array relationship in Go - Interactive Code Practice

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

Complete the code to create a slice from the array named arr.

Go
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1]
Drag options to blanks, or click blank then click option'
A[:3]
B[3:]
C[1:4]
D[5:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty slice like [5:] results in an empty slice, not an error.
Confusing slice indices and array indices.
2fill in blank
medium

Complete the code to append the value 6 to the slice named slice.

Go
slice := []int{1, 2, 3}
slice = append(slice, [1])
Drag options to blanks, or click blank then click option'
A6
B5
C7
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Appending a wrong number changes the slice content unexpectedly.
Forgetting to assign the result of append back to the slice.
3fill in blank
hard

Fix the error in the code to correctly create a slice from the array arr.

Go
arr := [4]int{10, 20, 30, 40}
slice := arr[1]
Drag options to blanks, or click blank then click option'
A[1:5]
B[2:6]
C[0:4]
D[4:4]
Attempts:
3 left
💡 Hint
Common Mistakes
Using an upper index greater than the array length causes runtime panic.
Using an empty slice like [4:4] results in an empty slice, not an error.
4fill in blank
hard

Fill both blanks to create a slice of the middle three elements from the array arr.

Go
arr := [5]int{5, 10, 15, 20, 25}
slice := arr[1][2]
Drag options to blanks, or click blank then click option'
A[1:
B2]
C4]
D[2:
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect indices that include too few or too many elements.
Forgetting that the end index is not included in the slice.
5fill in blank
hard

Fill all three blanks to create a new slice from arr and append the value 100.

Go
arr := [4]int{1, 2, 3, 4}
slice := append(arr[1][2], [3])
Drag options to blanks, or click blank then click option'
A[1:
B3]
C100
D[0:
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong slice indices that cause runtime errors.
Appending a value without assigning back to the slice.