An array in Go is a fixed-size collection of elements of the same type stored in contiguous memory locations.
Click to reveal answer
beginner
How do you declare an array of 5 integers in Go?
You declare it as: var arr [5]int. This creates an array named arr with 5 integer elements initialized to zero.
Click to reveal answer
beginner
How to initialize an array with values during declaration in Go?
You can initialize like this: arr := [3]int{10, 20, 30}. This creates an array of size 3 with elements 10, 20, and 30.
Click to reveal answer
beginner
What happens if you declare an array without initializing it in Go?
All elements are set to the zero value of the array's type. For integers, this means all elements are 0.
Click to reveal answer
intermediate
How to declare an array without specifying size but initializing it in Go?
Use arr := [...]int{1, 2, 3}. The compiler counts the elements and sets the array size automatically.
Click to reveal answer
How do you declare an array of 4 strings in Go?
Avar arr string[4]
Bvar arr [4]string
Carr := []string{4}
Darr := [4]string{}
✗ Incorrect
Option B correctly declares an array of 4 strings. Option D is also valid but initializes an empty array; C is the standard declaration.
What is the default value of elements in an uninitialized integer array in Go?
A0
Bnil
Cundefined
Dempty string
✗ Incorrect
Integer arrays default to 0 for all elements if not initialized.
How to declare and initialize an array with values 5, 10, 15 in Go without specifying size?
Aarr := [3]int{5, 10, 15}
Barr := []int{5, 10, 15}
Carr := [...]int{5, 10, 15}
Dvar arr []int = {5, 10, 15}
✗ Incorrect
Option C uses '...' to let the compiler determine the size automatically.
Which of these is NOT a valid way to declare an array in Go?
Avar arr [2]int
Barr := [2]int{1, 2}
Cvar arr []int
Darr := [2]int{1, 2, 3}
✗ Incorrect
Option D tries to initialize an array of size 2 with 3 elements, which is invalid.
What does the following code print?
var arr [3]int
fmt.Println(arr)
A[0 0 0]
B[1 2 3]
C[nil nil nil]
DCompilation error
✗ Incorrect
Uninitialized integer arrays have all elements set to 0.
Explain how to declare and initialize an array in Go with examples.
Think about how you create a fixed-size list of items in Go.
You got /4 concepts.
What happens if you declare an array but do not assign any values to it in Go?
Consider what Go does to keep arrays predictable and safe.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is the correct way to declare an array of 5 integers in Go?
easy
A. var arr []int = make([]int, 5)
B. var arr [5]int
C. arr := [5]int
D. arr := []int{5}
Solution
Step 1: Understand array declaration syntax in Go
Arrays in Go are declared with a fixed size inside square brackets, followed by the type, e.g., var arr [5]int.
Step 2: Analyze each option
var arr [5]int correctly declares an array of 5 integers. arr := [5]int uses shorthand but lacks the array literal {}, causing a syntax error. var arr []int = make([]int, 5) declares a slice, not an array. arr := []int{5} declares a slice with one element 5, not an array of size 5.
Final Answer:
var arr [5]int -> Option B
Quick Check:
Array declaration = var arr [5]int [OK]
Hint: Arrays use fixed size in brackets, slices do not [OK]
Common Mistakes:
Confusing slices with arrays
Using shorthand without initialization
Incorrect size placement
2. Which of the following is the correct syntax to initialize an array with values 1, 2, 3 in Go?
easy
A. arr := []int{1, 2, 3}
B. arr := [3]int(1, 2, 3)
C. var arr [3]int = {1, 2, 3}
D. var arr = [3]int{1, 2, 3}
Solution
Step 1: Review array initialization syntax
In Go, arrays are initialized with curly braces and values inside, e.g., [3]int{1, 2, 3}.
Step 2: Check each option
var arr = [3]int{1, 2, 3} uses correct syntax. arr := [3]int(1, 2, 3) uses parentheses which is invalid. var arr [3]int = {1, 2, 3} misses the type keyword before braces. arr := []int{1, 2, 3} declares a slice, not an array.
Final Answer:
var arr = [3]int{1, 2, 3} -> Option D
Quick Check:
Array init with braces = var arr = [3]int{1, 2, 3} [OK]
Hint: Use curly braces {} for array initialization [OK]
Common Mistakes:
Using parentheses instead of braces
Confusing slices with arrays
Omitting type in declaration
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
var arr = [4]int{10, 20}
fmt.Println(arr)
}
medium
A. [10 20 0 0]
B. [10 20]
C. [0 0 0 0]
D. Compilation error
Solution
Step 1: Understand partial array initialization
When an array is partially initialized, the remaining elements are set to zero values (0 for int).
Step 2: Analyze the given array
The array size is 4, but only first two elements are initialized as 10 and 20. The last two elements default to 0.
Hint: Uninitialized array elements default to zero [OK]
Common Mistakes:
Expecting only initialized elements in output
Thinking partial init causes error
Confusing slices with arrays
4. Identify the error in the following Go code snippet:
package main
func main() {
var arr [3]int = [3]int{1, 2}
}
medium
A. No error, code is correct
B. Array size mismatch error
C. Syntax error in array declaration
D. Missing import statement
Solution
Step 1: Check array size and initialization
The array is declared with size 3 but initialized with 2 values. Go allows this and fills the last element with zero.
Step 2: Verify syntax and imports
No syntax error exists. Import is not mandatory if no external packages are used. The code compiles fine.
Final Answer:
No error, code is correct -> Option A
Quick Check:
Partial init allowed, no import needed [OK]
Hint: Partial initialization is allowed; missing imports only if used [OK]
Common Mistakes:
Assuming size mismatch causes error
Expecting import for main package
Confusing slices with arrays
5. You want to declare an array of 5 integers in Go and initialize only the last element to 100, leaving others as zero. Which code snippet achieves this correctly?
hard
A. var arr = [5]int{0: 100}
B. var arr = [5]int{5: 100}
C. var arr = [5]int{4: 100}
D. var arr = [5]int{100}
Solution
Step 1: Understand indexed initialization in arrays
Go allows initializing specific elements by index using syntax like {index: value}. Indexing starts at 0.
Step 2: Analyze each option
var arr = [5]int{4: 100} initializes the element at index 4 (the last element in a 5-element array) to 100, others default to 0. var arr = [5]int{5: 100} uses index 5 which is out of range. var arr = [5]int{100} initializes only the first element. var arr = [5]int{0: 100} initializes index 0 to 100, not the last.
Final Answer:
var arr = [5]int{4: 100} -> Option C
Quick Check:
Use index 4 for last element in 5-size array [OK]
Hint: Index starts at 0; last element is index size-1 [OK]
Common Mistakes:
Using out-of-range index
Initializing first element instead of last
Omitting index for specific element
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.