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 like this: var arr [5]int. This creates an array named arr that can hold 5 integers.
Click to reveal answer
intermediate
What happens if you try to assign more elements than the declared size of an array in Go?
You will get a compile-time error because arrays have fixed size and cannot hold more elements than declared.
Click to reveal answer
beginner
How can you declare and initialize an array in one line in Go?
You can write: arr := [3]int{10, 20, 30}. This creates an array of size 3 with values 10, 20, and 30.
Click to reveal answer
intermediate
What is the difference between an array and a slice in Go?
An array has a fixed size known at compile time, while a slice is a flexible, dynamic view over an array that can grow or shrink.
Click to reveal answer
How do you declare an array of 4 strings in Go?
Aarr := [4]string{}
Bvar arr [4]string
Carr := make([]string, 4)
Dvar arr []string
✗ Incorrect
Option B declares a fixed-size array of 4 strings. Option D and C declare slices, not arrays. Option A is invalid syntax.
What is the default value of elements in a newly declared integer array in Go?
Aundefined
Bnil
C0
Dempty string
✗ Incorrect
Integer arrays are initialized with zero values, so each element is 0 by default.
Which of these is a valid way to declare and initialize an array in Go?
Aarr := []int{1, 2, 3}
Barr := array{1, 2, 3}
Cvar arr = [3]int(1, 2, 3)
Darr := [3]int{1, 2, 3}
✗ Incorrect
Option D correctly declares and initializes an array. Option A is a slice. Options C and D are invalid syntax.
What happens if you try to access an index outside the declared size of an array in Go?
ARuntime panic
BCompile-time error
CReturns zero value
DReturns last element
✗ Incorrect
Accessing out-of-range index causes a runtime panic in Go.
Which statement about Go arrays is true?
AArrays have fixed size and hold elements of the same type.
BArrays hold elements of different types.
CArrays can change size after declaration.
DArrays are pointers to slices.
✗ Incorrect
Arrays in Go have fixed size and all elements must be of the same type.
Explain how to declare and initialize an array in Go with examples.
Think about using var and shorthand := with curly braces.
You got /4 concepts.
Describe the difference between arrays and slices in Go.
Consider how flexible each type is and how they relate.
You got /4 concepts.
Practice
(1/5)
1. What does the following Go code declare? var arr [3]int
easy
A. A slice of integers with length 3
B. An array of 3 integers initialized with zero values
C. A pointer to an integer array
D. A map with integer keys and values
Solution
Step 1: Understand the syntax var arr [3]int
This declares a fixed-size array named arr with 3 elements, each of type int.
Step 2: Recognize default initialization
In Go, arrays are initialized with zero values for their type, so all 3 integers start as 0.
Final Answer:
An array of 3 integers initialized with zero values -> Option B
Quick Check:
Array declaration = fixed size + type + zero values [OK]
Hint: Remember: [n]type means fixed size array of n elements [OK]
Common Mistakes:
Confusing array with slice declaration
Thinking it creates a pointer or map
Assuming uninitialized values are random
2. Which of the following is the correct way to declare an array of 4 strings in Go?
easy
A. var arr [4]string
B. var arr []string{4}
C. arr := [4]string{}
D. var arr string[4]
Solution
Step 1: Check Go array declaration syntax
Arrays are declared with var name [size]type. So var arr [4]string is correct.
Step 2: Analyze other options
var arr []string{4} uses slice syntax incorrectly. arr := [4]string{} is valid short declaration syntax in Go. var arr string[4] reverses size and type, which is invalid.
Final Answer:
var arr [4]string -> Option A
Quick Check:
Array syntax = var name [size]type [OK]
Hint: Array size goes inside brackets before type [OK]
Common Mistakes:
Using slice syntax instead of array syntax
Placing size after type
Missing var keyword or wrong initialization
3. What is the output of this Go program?
package main
import "fmt"
func main() {
var nums = [3]int{10, 20, 30}
fmt.Println(nums[1])
}
medium
A. 10
B. 0
C. 30
D. 20
Solution
Step 1: Understand array initialization
The array nums has 3 integers: 10 at index 0, 20 at index 1, and 30 at index 2.
Step 2: Access element at index 1
Printing nums[1] outputs the second element, which is 20.
Final Answer:
20 -> Option D
Quick Check:
Array index starts at 0, nums[1] = 20 [OK]
Hint: Array indexes start at 0; second element is index 1 [OK]
Common Mistakes:
Confusing index 1 with index 0
Expecting output 10 or 30 instead of 20
Thinking array is zero-indexed but printing wrong element
4. Identify the error in this Go array declaration:
var arr [3]int = {1, 2, 3}
medium
A. Array initialization syntax is incorrect
B. Braces {} should be replaced with []
C. Missing keyword 'var'
D. Array size must be omitted
Solution
Step 1: Review array initialization syntax
In Go, when initializing an array with values, the correct syntax is var arr = [3]int{1, 2, 3} or arr := [3]int{1, 2, 3}.
Step 2: Identify the error
The code uses var arr [3]int = {1, 2, 3} which misses the array type on the right side before the braces. This causes a syntax error.
Final Answer:
Array initialization syntax is incorrect -> Option A
Quick Check:
Array init needs type before braces [OK]
Hint: Array init needs type before braces, like [3]int{...} [OK]
Common Mistakes:
Omitting array type on right side during initialization
Using wrong braces or brackets
Trying to assign without proper syntax
5. How can you declare and initialize a 2D array of integers with 2 rows and 3 columns in Go?
hard
A. var matrix [2][3]int = [2][3]int{{1,2,3},{4,5,6}}
B. var matrix [][]int = {{1,2,3},{4,5,6}}
C. matrix := [2][3]int{{1,2,3},{4,5,6}}
D. var matrix [3][2]int = {{1,2},{3,4},{5,6}}
Solution
Step 1: Understand 2D array declaration
A 2D array with 2 rows and 3 columns is declared as [2][3]int. Both dimensions must be specified.
Step 2: Check initialization syntax
matrix := [2][3]int{{1,2,3},{4,5,6}} uses short declaration with := and correctly initializes the 2D array with two rows of three integers each.
Step 3: Analyze other options
var matrix [2][3]int = [2][3]int{{1,2,3},{4,5,6}} is correct syntax but uses explicit var with type on both sides (valid but less common). var matrix [][]int = {{1,2,3},{4,5,6}} is a slice of slices, not an array. var matrix [3][2]int = {{1,2},{3,4},{5,6}} has dimensions reversed (3 rows, 2 columns).
Final Answer:
matrix := [2][3]int{{1,2,3},{4,5,6}} -> Option C
Quick Check:
2D array = [rows][cols]type with nested braces [OK]
Hint: Use [rows][cols]type with nested braces to initialize [OK]
Common Mistakes:
Confusing slices with arrays
Swapping row and column sizes
Missing nested braces for 2D arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.