Challenge - 5 Problems
Array Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Remember, len() returns the number of elements in an array.
✗ Incorrect
The array has 5 elements, so len(arr) returns 5.
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Slices also have a length accessible by len().
✗ Incorrect
The slice has 3 elements, so len(slice) returns 3.
❓ Predict Output
advanced2: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])) }
Attempts:
2 left
💡 Hint
len(arr) returns the number of rows, len(arr[0]) returns the number of columns.
✗ Incorrect
The outer array has length 2, each inner array has length 3.
❓ Predict Output
advanced2: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]) }
Attempts:
2 left
💡 Hint
Accessing arr[3] is out of bounds for an array of length 3.
✗ Incorrect
The program compiles but panics at runtime due to out-of-range index.
🧠 Conceptual
expert2: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}
Attempts:
2 left
💡 Hint
The ... tells Go to count the elements automatically.
✗ Incorrect
The ... means Go counts the elements in the initializer, which are 7.