0
0
Goprogramming~5 mins

Array declaration in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an array in Go?
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
What is the default value of elements in a newly declared integer array in Go?
Aundefined
Bnil
C0
Dempty string
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}
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
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.
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.