0
0
Goprogramming~10 mins

Array length 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 length of the array.

Go
package main
import "fmt"
func main() {
	arr := [5]int{1, 2, 3, 4, 5}
	fmt.Println(len([1]))
}
Drag options to blanks, or click blank then click option'
Aarr
Barray
Clen
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'arr'.
Passing a number instead of the array variable.
Using 'len' as the argument inside len().
2fill in blank
medium

Complete the code to store the length of the array in a variable.

Go
package main
import "fmt"
func main() {
	nums := [3]int{10, 20, 30}
	length := [1](nums)
	fmt.Println(length)
}
Drag options to blanks, or click blank then click option'
Asize
Blen
Ccount
Dcap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cap' which returns capacity, not length.
Using undefined functions like 'size' or 'count'.
3fill in blank
hard

Fix the error in the code to correctly print the length of the array.

Go
package main
import "fmt"
func main() {
	values := [4]int{5, 6, 7, 8}
	fmt.Println([1](values))
}
Drag options to blanks, or click blank then click option'
Alength
Bsize
Clen
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' or 'length' which are not built-in functions in Go.
Trying to access a property like values.length which does not exist.
4fill in blank
hard

Fill both blanks to create a map with words as keys and their lengths as values, but only 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 words shorter than or equal to 3 characters.
Not using len() to get the length.
5fill in blank
hard

Fill all three blanks to create a map of words to their lengths, including only words longer than 2 characters.

Go
package main
import "fmt"
func main() {
	words := []string{"go", "play", "fun", "code"}
	lengths := map[string]int{
		[1]: [2],
		[3]: len("code"),
	}
	fmt.Println(lengths)
}
Drag options to blanks, or click blank then click option'
A"play"
Blen("play")
C"code"
D"go"
Attempts:
3 left
💡 Hint
Common Mistakes
Including words that are too short.
Not matching keys with their correct lengths.