0
0
Goprogramming~20 mins

Array limitations in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery in Go
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 with arrays?

Consider the following Go program. What will it print?

Go
package main
import "fmt"
func main() {
    var arr [3]int
    fmt.Println(len(arr))
    fmt.Println(cap(arr))
}
A
0
0
B
0
3
C
3
3
D
3
0
Attempts:
2 left
💡 Hint

Remember that arrays in Go have fixed size known at compile time.

Predict Output
intermediate
2:00remaining
What happens if you assign arrays of different sizes?

What error or output results from this Go code?

Go
package main
func main() {
    var a [3]int
    var b [4]int
    a = b
}
Acompile-time error: cannot use b (type [4]int) as type [3]int in assignment
Bruntime panic: array size mismatch
Ccompiles and runs with no output
Dcompiles but runtime error when assigning
Attempts:
2 left
💡 Hint

Arrays of different sizes are different types in Go.

🔧 Debug
advanced
2:00remaining
Why does this Go code cause an error when passing an array to a function?

Identify the problem in this code and what error it causes.

Go
package main
import "fmt"
func modify(arr [3]int) {
    arr[0] = 100
}
func main() {
    a := [3]int{1,2,3}
    modify(a)
    fmt.Println(a)
}
AOutput is [1 2 3] because array is passed by value, so original is unchanged
BOutput is [100 2 3] because array is passed by reference
CCompile-time error: cannot assign to array element
DRuntime panic: index out of range
Attempts:
2 left
💡 Hint

Think about how arrays are passed to functions in Go.

🧠 Conceptual
advanced
1:30remaining
What is a key limitation of arrays in Go compared to slices?

Choose the best description of a limitation of arrays in Go.

AArrays can only hold elements of type int
BArrays have fixed size and cannot be resized after declaration
CArrays automatically grow when elements are appended
DArrays support dynamic memory allocation
Attempts:
2 left
💡 Hint

Think about what happens if you want to add more elements than the array size.

Predict Output
expert
2:30remaining
What is the output of this Go code involving array comparison?

What does this program print?

Go
package main
import "fmt"
func main() {
    a := [3]int{1, 2, 3}
    b := [3]int{1, 2, 3}
    c := [3]int{3, 2, 1}
    fmt.Println(a == b)
    fmt.Println(a == c)
}
A
false
true
B
false
false
C
true
true
D
true
false
Attempts:
2 left
💡 Hint

Arrays in Go can be compared element-wise if they have the same type and size.