Bird
Raised Fist0
Goprogramming~20 mins

Array initialization in Go - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly declares an array of 5 integers in Go?
easy
A. var arr int[5]
B. var arr [5]int
C. arr := []int{5}
D. arr := [5]int{1, 2, 3, 4}

Solution

  1. Step 1: Understand Go array declaration syntax

    In Go, arrays are declared with the syntax: var name [size]type.
  2. Step 2: Check each option

    A: var arr int[5] wrong syntax (type before size). B: arr := []int{5} is a slice, not an array. C: var arr [5]int correct syntax. D: arr := [5]int{1, 2, 3, 4} too few initializers (4 < 5), compilation error.
  3. Final Answer:

    var arr [5]int -> Option B
  4. Quick Check:

    Array declaration = var arr [5]int [OK]
Hint: Remember: var name [size]type declares an array [OK]
Common Mistakes:
  • Confusing slices with arrays
  • Using wrong syntax like int[5]
  • Missing var keyword for array declaration
2. Which of these is the correct way to initialize an array of 3 strings with values "a", "b", "c" in Go?
easy
A. arr := [3]string{"a", "b", "c"}
B. arr := []string{"a", "b", "c"}
C. var arr [3]string = {"a", "b", "c"}
D. arr := [3]string{"a", "b", c}

Solution

  1. Step 1: Identify correct array initialization syntax

    Arrays use [size]type{values} syntax. arr := [3]string{"a", "b", "c"} matches this exactly.
  2. Step 2: Check other options for errors

    arr := []string{"a", "b", "c"} is a slice, not an array. var arr [3]string = {"a", "b", "c"} uses invalid initialization with = and braces. arr := [3]string{"a", "b", c} misses quotes around "c" causing syntax error.
  3. Final Answer:

    arr := [3]string{"a", "b", "c"} -> Option A
  4. Quick Check:

    Array init with values = [3]string{"a", "b", "c"} [OK]
Hint: Use [size]type{values} for array initialization [OK]
Common Mistakes:
  • Using slice syntax instead of array
  • Missing quotes around string elements
  • Incorrect use of = with array literals
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
  arr := [4]int{1, 2}
  fmt.Println(arr)
}
medium
A. [1 2 0 0]
B. [1 2]
C. [0 0 0 0]
D. Compilation error

Solution

  1. Step 1: Understand partial array initialization

    When fewer elements are provided than the array size, Go fills remaining elements with zero values.
  2. Step 2: Apply to given array

    Array size is 4, initialized with 1 and 2. Remaining two elements become 0 by default.
  3. Final Answer:

    [1 2 0 0] -> Option A
  4. Quick Check:

    Partial init fills rest with zero [OK]
Hint: Uninitialized array elements default to zero [OK]
Common Mistakes:
  • Assuming array length matches initializer length
  • Expecting error for fewer initial values
  • Confusing slices with arrays
4. Identify the error in this Go array initialization:
var arr [3]int = [3]int{1, 2, 3, 4}
medium
A. No error, code is correct
B. Missing var keyword
C. Incorrect type declaration
D. Array size mismatch with initializer

Solution

  1. Step 1: Check array size and initializer length

    Array declared with size 3 but initializer has 4 elements.
  2. Step 2: Understand Go array rules

    Go requires initializer length to match array size exactly, else compilation error occurs.
  3. Final Answer:

    Array size mismatch with initializer -> Option D
  4. Quick Check:

    Initializer length must equal array size [OK]
Hint: Initializer length must match array size exactly [OK]
Common Mistakes:
  • Ignoring extra elements in initializer
  • Assuming Go auto-expands array size
  • Confusing slices with arrays
5. You want to create an array of 5 integers where the first two elements are 10 and 20, and the rest are zeros. Which code snippet correctly achieves this and prints the array?
hard
A. arr := [5]int{10, 20, 30} fmt.Println(arr)
B. arr := [3]int{10, 20} fmt.Println(arr)
C. arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr)
D. arr := []int{10, 20} fmt.Println(arr)

Solution

  1. Step 1: Understand array initialization with explicit zeros

    To ensure the last three elements are zeros explicitly, include them in the initializer.
  2. Step 2: Check each option

    B: arr := [3]int{10, 20} fmt.Println(arr) wrong size 3, not 5. A: arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) explicitly sets zeros. C: arr := [5]int{10, 20, 30} fmt.Println(arr) third element 30, not zero. D: arr := []int{10, 20} fmt.Println(arr) is a slice, not array.
  3. Step 3: Choose the best explicit solution

    A clearly shows all elements matching the requirement exactly.
  4. Final Answer:

    arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) -> Option C
  5. Quick Check:

    Explicit zeros in initializer = arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) [OK]
Hint: Explicitly list zeros to avoid confusion [OK]
Common Mistakes:
  • Assuming missing elements are always zero without clarity
  • Using slices instead of arrays
  • Adding unintended values in initializer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard