0
0
Goprogramming~10 mins

Slicing operations 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 of integers from 1 to 5.

Go
numbers := []int[1]
Drag options to blanks, or click blank then click option'
A{1, 2, 3, 4, 5}
B[1, 2, 3, 4, 5]
C(1, 2, 3, 4, 5)
D<1, 2, 3, 4, 5>
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of curly braces {} for slice elements.
Using parentheses () or angle brackets <> which are invalid in Go slice literals.
2fill in blank
medium

Complete the code to get a slice of the first three elements from the slice 'numbers'.

Go
firstThree := numbers[1]
Drag options to blanks, or click blank then click option'
A[1:4]
B[1:3]
C[0:3]
D[0:2]
Attempts:
3 left
💡 Hint
Common Mistakes
Starting slice at 1 which skips the first element.
Ending slice at 2 which only gets two elements.
3fill in blank
hard

Fix the error in the code to get the last two elements of the slice 'numbers'.

Go
lastTwo := numbers[1]
Drag options to blanks, or click blank then click option'
A[len(numbers)-2:]
B[len(numbers):]
C[:len(numbers)-2]
D[len(numbers)-3:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(numbers): which is invalid syntax.
Slicing from the wrong index which returns incorrect elements.
4fill in blank
hard

Fill both blanks to create a slice of elements from index 2 up to 4 (exclusive) from 'numbers'.

Go
middle := numbers[1][2]
Drag options to blanks, or click blank then click option'
A[2:
B]
C4]
D(
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Forgetting the colon between indices.
5fill in blank
hard

Fill both blanks to create a slice of the last three elements from 'numbers' using slicing syntax.

Go
lastThree := numbers[1][2]
Drag options to blanks, or click blank then click option'
A[
Blen(numbers)-3
C:]
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using closing bracket before colon.
Using incorrect start index.