0
0
Goprogramming~20 mins

Array initialization in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array initialization with default values
What is the output of this Go program?
package main
import "fmt"
func main() {
  var arr [4]int
  fmt.Println(arr)
}
Go
package main
import "fmt"
func main() {
  var arr [4]int
  fmt.Println(arr)
}
A[0 1 2 3]
B[1 2 3 4]
C[0 0 0 0]
D[4 4 4 4]
Attempts:
2 left
💡 Hint
In Go, arrays of integers are initialized with zero values by default.
Predict Output
intermediate
2:00remaining
Output of array initialization with explicit values
What does this Go program print?
package main
import "fmt"
func main() {
  arr := [3]string{"go", "lang", "rocks"}
  fmt.Println(arr)
}
Go
package main
import "fmt"
func main() {
  arr := [3]string{"go", "lang", "rocks"}
  fmt.Println(arr)
}
A[go lang rocks rocks]
B["go" "lang" "rocks"]
C[go lang]
D[go lang rocks]
Attempts:
2 left
💡 Hint
Arrays print their elements separated by spaces inside square brackets.
Predict Output
advanced
2:00remaining
Output of partially initialized array
What is the output of this Go program?
package main
import "fmt"
func main() {
  arr := [5]int{1, 2}
  fmt.Println(arr)
}
Go
package main
import "fmt"
func main() {
  arr := [5]int{1, 2}
  fmt.Println(arr)
}
A[1 2 0 0 0]
B[1 2 3 4 5]
C[0 0 0 0 0]
D[1 2]
Attempts:
2 left
💡 Hint
Uninitialized elements in an array get zero values.
Predict Output
advanced
2:00remaining
Output of array initialization with index keys
What does this Go program print?
package main
import "fmt"
func main() {
  arr := [4]int{1: 10, 3: 30}
  fmt.Println(arr)
}
Go
package main
import "fmt"
func main() {
  arr := [4]int{1: 10, 3: 30}
  fmt.Println(arr)
}
A[10 30 0 0]
B[0 10 0 30]
C[1 10 3 30]
D[10 0 30 0]
Attempts:
2 left
💡 Hint
Elements not explicitly set get zero values.
🧠 Conceptual
expert
2:00remaining
Length of array initialized with ellipsis
What is the length of the array declared in this Go code?
arr := [...]int{5, 10, 15, 20}
Go
package main
import "fmt"
func main() {
  arr := [...]int{5, 10, 15, 20}
  fmt.Println(len(arr))
}
A4
B3
C5
D0
Attempts:
2 left
💡 Hint
The ellipsis lets Go count the elements automatically.