0
0
Goprogramming~10 mins

Accessing array elements 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 print the first element of the array.

Go
package main
import "fmt"
func main() {
    arr := [3]int{10, 20, 30}
    fmt.Println(arr[[1]])
}
Drag options to blanks, or click blank then click option'
A(0)
B<0>
C{0}
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Using an index starting at 1 instead of 0.
2fill in blank
medium

Complete the code to print the last element of the array.

Go
package main
import "fmt"
func main() {
    arr := [4]string{"a", "b", "c", "d"}
    fmt.Println(arr[[1]])
}
Drag options to blanks, or click blank then click option'
A3
B4
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 4 which is out of range.
Using negative indexes which Go does not support.
3fill in blank
hard

Fix the error in the code to correctly access the second element of the array.

Go
package main
import "fmt"
func main() {
    nums := [5]int{1, 2, 3, 4, 5}
    fmt.Println(nums[[1]])
}
Drag options to blanks, or click blank then click option'
A<1>
B(1)
C1
D{1}
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Using index 2 instead of 1 for the second element.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths for words longer than 3 characters.

Go
package main
import "fmt"
func main() {
    words := []string{"go", "code", "play", "fun"}
    lengths := map[string]int{
        [1]: [2],
    }
    fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
A"code"
Blen("code")
C"go"
Dlen("go")
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong word as key.
Not using the len function for the value.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 2 characters.

Go
package main
import (
    "fmt"
    "strings"
)
func main() {
    words := []string{"go", "code", "play", "fun"}
    lengths := map[string]int{
        [1]: [2],
        [3]: len("play"),
    }
    fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
Astrings.ToUpper("code")
Blen("code")
Cstrings.ToUpper("play")
Dlen("play")
Attempts:
3 left
💡 Hint
Common Mistakes
Not using strings.ToUpper for keys.
Mixing up keys and values.