0
0
Goprogramming~20 mins

Why arrays are needed in Go - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array initialization and access
What is the output of this Go program that initializes an array and prints its elements?
Go
package main
import "fmt"
func main() {
    var arr [3]int = [3]int{10, 20, 30}
    for i := 0; i < len(arr); i++ {
        fmt.Print(arr[i], " ")
    }
}
A10 20 30
B10 20 30 0
C0 0 0
DCompilation error
Attempts:
2 left
💡 Hint
Remember arrays hold fixed-size collections of elements of the same type.
🧠 Conceptual
intermediate
1:30remaining
Why use arrays instead of separate variables?
Why are arrays useful compared to using many separate variables for similar data?
AArrays automatically sort the data for you.
BArrays allow grouping multiple values of the same type under one name for easy management.
CArrays use less memory than separate variables.
DArrays can store different types of data in one variable.
Attempts:
2 left
💡 Hint
Think about how you handle many similar items in real life, like a row of mailboxes.
Predict Output
advanced
2:00remaining
Output when modifying array elements
What is the output of this Go program that modifies an array element?
Go
package main
import "fmt"
func main() {
    arr := [4]int{1, 2, 3, 4}
    arr[2] = 10
    fmt.Println(arr)
}
A[1 2 10 4]
B[1 2 3 4]
C[10 2 3 4]
DRuntime error
Attempts:
2 left
💡 Hint
Arrays allow changing values at specific positions by index.
🔧 Debug
advanced
2:00remaining
Identify the error in array declaration
What error does this Go code produce and why?
Go
package main
func main() {
    var arr [3]int = [4]int{1, 2, 3, 4}
}
Ano error, compiles fine
Bindex out of range error
Csyntax error: missing colon
Dcannot use [4]int literal as [3]int value in assignment
Attempts:
2 left
💡 Hint
Check if the array sizes on both sides match.
🚀 Application
expert
2:30remaining
Count how many elements in an array are greater than 5
What is the value of count after running this Go code?
Go
package main
import "fmt"
func main() {
    arr := [5]int{3, 7, 2, 9, 5}
    count := 0
    for _, v := range arr {
        if v > 5 {
            count++
        }
    }
    fmt.Println(count)
}
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Look at each element and count only those greater than 5.