Bird
Raised Fist0
Goprogramming~10 mins

Array length in Go - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the len() function return when used with an array in Go?
easy
A. The memory size of the array in bytes
B. The number of elements in the array
C. The capacity of the array's underlying slice
D. The last element of the array

Solution

  1. Step 1: Understand the purpose of len() in Go

    The len() function returns the count of elements in arrays, slices, maps, and strings.
  2. Step 2: Apply len() to an array

    When used on an array, len() returns the fixed number of elements it holds.
  3. Final Answer:

    The number of elements in the array -> Option B
  4. Quick Check:

    len(array) = number of elements [OK]
Hint: len() always gives count of elements in an array [OK]
Common Mistakes:
  • Confusing len() with capacity
  • Thinking len() returns memory size
  • Assuming len() returns last element
2. Which of the following is the correct way to get the length of an array named arr in Go?
easy
A. len(arr)
B. arr.length
C. size(arr)
D. length(arr)

Solution

  1. Step 1: Recall Go syntax for length

    In Go, the built-in function len() is used to get the length of arrays and slices.
  2. Step 2: Check each option

    Only len(arr) is valid syntax; others are invalid or do not exist in Go.
  3. Final Answer:

    len(arr) -> Option A
  4. Quick Check:

    Use len() function for array length [OK]
Hint: Use len(array) to get length, not dot or other functions [OK]
Common Mistakes:
  • Using dot notation like arr.length
  • Using non-existent functions like length() or size()
  • Confusing with other languages' syntax
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
    arr := [4]int{10, 20, 30, 40}
    fmt.Println(len(arr))
}
medium
A. 0
B. 3
C. 4
D. Compilation error

Solution

  1. Step 1: Analyze the array declaration

    The array arr is declared with 4 elements: 10, 20, 30, 40.
  2. Step 2: Understand len(arr)

    The len() function returns the number of elements, which is 4.
  3. Final Answer:

    4 -> Option C
  4. Quick Check:

    len([4]int{...}) = 4 [OK]
Hint: Count elements inside [ ] to find length [OK]
Common Mistakes:
  • Counting elements incorrectly
  • Thinking len() returns last index (3)
  • Expecting runtime error
4. Identify the error in this Go code snippet:
package main
import "fmt"
func main() {
    arr := [3]int{1, 2, 3}
    fmt.Println(len(arr))
    fmt.Println(arr[3])
}
medium
A. fmt.Println cannot print arrays
B. len(arr) is invalid for arrays
C. Array declaration syntax is wrong
D. Accessing arr[3] causes an index out of range error

Solution

  1. Step 1: Check array length and indexing

    The array arr has length 3, so valid indices are 0, 1, and 2.
  2. Step 2: Identify invalid index access

    Accessing arr[3] is out of range and causes a runtime panic.
  3. Final Answer:

    Accessing arr[3] causes an index out of range error -> Option D
  4. Quick Check:

    Array indices go from 0 to len-1 [OK]
Hint: Array indices start at 0, max index = length-1 [OK]
Common Mistakes:
  • Assuming len(arr) is invalid
  • Thinking fmt.Println can't print arrays
  • Confusing array length with capacity
5. Given two arrays in Go:
arr1 := [5]int{1, 2, 3, 4, 5}
arr2 := [3]int{10, 20, 30}
Which expression correctly checks if arr2 has fewer elements than arr1?
hard
A. len(arr2) < len(arr1)
B. len(arr1) < len(arr2)
C. arr2.length < arr1.length
D. size(arr2) < size(arr1)

Solution

  1. Step 1: Identify lengths of both arrays

    arr1 has length 5, arr2 has length 3.
  2. Step 2: Write correct comparison syntax

    Use len() to get lengths. To check if arr2 is smaller, compare len(arr2) < len(arr1).
  3. Final Answer:

    len(arr2) < len(arr1) -> Option A
  4. Quick Check:

    len(arr2) = 3 < 5 = len(arr1) [OK]
Hint: Use len() and compare lengths directly [OK]
Common Mistakes:
  • Using wrong function names like length or size
  • Reversing comparison order
  • Using dot notation for length
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard