0
0
Goprogramming~20 mins

Array length in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Go code?
Consider the following Go program. What will it print when run?
Go
package main
import "fmt"
func main() {
    arr := [5]int{1, 2, 3, 4, 5}
    fmt.Println(len(arr))
}
A0
B5
C6
DCompilation error
Attempts:
2 left
💡 Hint
Remember, len() returns the number of elements in an array.
Predict Output
intermediate
2:00remaining
What is the output of this Go code with slice?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    slice := []int{10, 20, 30}
    fmt.Println(len(slice))
}
ACompilation error
B0
C3
DRuntime panic
Attempts:
2 left
💡 Hint
Slices also have a length accessible by len().
Predict Output
advanced
2:00remaining
What is the output of this Go code with multidimensional array?
What will this program print?
Go
package main
import "fmt"
func main() {
    arr := [2][3]int{{1,2,3},{4,5,6}}
    fmt.Println(len(arr))
    fmt.Println(len(arr[0]))
}
ACompilation error
B3\n2
C6\n0
D2\n3
Attempts:
2 left
💡 Hint
len(arr) returns the number of rows, len(arr[0]) returns the number of columns.
Predict Output
advanced
2:00remaining
What error does this Go code produce?
What happens when you run this Go code?
Go
package main
import "fmt"
func main() {
    var arr [3]int
    fmt.Println(len(arr))
    fmt.Println(arr[3])
}
ARuntime panic: index out of range
B0\n0
CCompilation error: invalid array index
D3\n0
Attempts:
2 left
💡 Hint
Accessing arr[3] is out of bounds for an array of length 3.
🧠 Conceptual
expert
2:00remaining
How many elements are in this Go array?
Given this Go array declaration, how many elements does it contain? var arr = [...]int{1, 2, 3, 4, 5, 6, 7}
A7
B0
CCompilation error
DDepends on runtime
Attempts:
2 left
💡 Hint
The ... tells Go to count the elements automatically.