Challenge - 5 Problems
Array Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of range loop with index and value
What is the output of this Go program?
Go
package main import "fmt" func main() { arr := [3]int{10, 20, 30} for i, v := range arr { fmt.Printf("%d:%d ", i, v) } }
Attempts:
2 left
💡 Hint
Remember that range returns index and value in that order.
✗ Incorrect
The range keyword returns two values: the index and the element at that index. The fmt.Printf prints them as index:value pairs separated by spaces.
❓ Predict Output
intermediate2:00remaining
Output when ignoring index in range loop
What will this Go program print?
Go
package main import "fmt" func main() { arr := [4]string{"a", "b", "c", "d"} for _, v := range arr { fmt.Print(v) } }
Attempts:
2 left
💡 Hint
The underscore _ ignores the index, so only values are printed.
✗ Incorrect
The loop ignores the index and prints each string value directly without spaces or newlines.
🔧 Debug
advanced2:00remaining
Identify the error in this array iteration
What error will this Go code produce when compiled?
Go
package main import "fmt" func main() { arr := [3]int{1, 2, 3} for i, v := range arr { fmt.Println(i + v) } fmt.Println(arr[3]) }
Attempts:
2 left
💡 Hint
Check the array length and the index used after the loop.
✗ Incorrect
The array has length 3, so valid indices are 0,1,2. Accessing arr[3] causes a runtime panic: index out of range.
❓ Predict Output
advanced2:00remaining
Output of modifying array elements inside range loop
What is the output of this Go program?
Go
package main import "fmt" func main() { arr := [3]int{1, 2, 3} for i, v := range arr { v = v * 2 fmt.Print(v, " ") } fmt.Println() fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Remember that v is a copy of the element, not the element itself.
✗ Incorrect
The range loop copies each element into v. Modifying v does not change the original array. So printing v shows doubled values, but the array remains unchanged.
🧠 Conceptual
expert2:00remaining
Number of iterations in nested array loops
Given this Go code, how many times will the innermost print statement execute?
Go
package main import "fmt" func main() { arr := [2][3]int{{1,2,3},{4,5,6}} count := 0 for _, inner := range arr { for i := range inner { fmt.Println(inner[i]) count++ } } fmt.Println("Total:", count) }
Attempts:
2 left
💡 Hint
Count elements in each inner array and multiply by number of outer arrays.
✗ Incorrect
The outer array has 2 elements, each inner array has 3 elements. The inner loop runs 3 times per outer iteration, total 2*3=6 times.