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 and initialize an array of 3 integers with values 1, 2, and 3 in Go?
You can declare and initialize it like this:
var arr = [3]int{1, 2, 3}
Click to reveal answer
beginner
What happens if you declare an array without initializing it in Go?
The array elements are automatically set to the zero value of the element type. For integers, this is 0.
Click to reveal answer
intermediate
How can you let Go infer the size of an array when initializing it?
Use [...] instead of a fixed size. Example:
arr := [...]int{4, 5, 6}
lets Go count the elements and set the size to 3.
Click to reveal answer
intermediate
Can you initialize an array with some elements and leave others to zero values in Go?
Yes. You can specify values for some indices and the rest will be zero. Example:
arr := [5]int{0: 10, 3: 20}
sets index 0 to 10, index 3 to 20, others to 0.
Click to reveal answer
How do you declare an array of 4 strings in Go with all elements initialized to empty strings?
Avar arr []string = {"", "", "", ""}
Bvar arr [4]string
Carr := [4]string{"", "", "", ""}
Darr := []string{4}
✗ Incorrect
Declaring 'var arr [4]string' creates an array of 4 strings with all elements set to the zero value, which is an empty string.
What does this code do? arr := [...]int{1, 2, 3, 4}
ADeclares an array of 4 integers with values 1, 2, 3, 4
BDeclares an array of size 3 with values 1, 2, 3
CDeclares a slice of 4 integers
DDeclares a pointer to an array
✗ Incorrect
The '...' lets Go count the elements and create an array of size 4 with the given values.
What is the zero value of an int array element in Go?
Aempty string
Bnil
Cundefined
D0
✗ Incorrect
The zero value for int type elements is 0.
How do you initialize only the first and last elements of a 5-element int array in Go?
Aarr := [5]int{1, 0, 0, 0, 5}
Barr := [5]int{0: 1, 4: 5}
CBoth A and B
DNeither A nor B
✗ Incorrect
Both ways correctly initialize the first and last elements; the rest are zero.
Which of these is NOT a valid way to declare an array in Go?
Avar arr []int = [3]int{1, 2, 3}
Barr := [...]int{1, 2, 3}
Carr := [3]int{1, 2, 3}
Dvar arr [3]int
✗ Incorrect
You cannot assign an array to a slice variable directly without conversion.
Explain how to declare and initialize an array in Go with specific values.
Think about using [size]type{values} or [...]type{values}.
You got /3 concepts.
Describe what happens when you declare an array without giving initial values in Go.
Consider what Go sets each element to automatically.
You got /3 concepts.
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
Step 1: Understand Go array declaration syntax
In Go, arrays are declared with the syntax: var name [size]type.
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.
Final Answer:
var arr [5]int -> Option B
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?
Arrays use [size]type{values} syntax. arr := [3]string{"a", "b", "c"} matches this exactly.
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.
Final Answer:
arr := [3]string{"a", "b", "c"} -> Option A
Quick Check:
Array init with values = [3]string{"a", "b", "c"} [OK]
Hint: Use [size]type{values} for array initialization [OK]
When fewer elements are provided than the array size, Go fills remaining elements with zero values.
Step 2: Apply to given array
Array size is 4, initialized with 1 and 2. Remaining two elements become 0 by default.
Final Answer:
[1 2 0 0] -> Option A
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
Step 1: Check array size and initializer length
Array declared with size 3 but initializer has 4 elements.
Step 2: Understand Go array rules
Go requires initializer length to match array size exactly, else compilation error occurs.
Final Answer:
Array size mismatch with initializer -> Option D
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
Step 1: Understand array initialization with explicit zeros
To ensure the last three elements are zeros explicitly, include them in the initializer.
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.
Step 3: Choose the best explicit solution
A clearly shows all elements matching the requirement exactly.