0
0
Goprogramming~5 mins

Array length in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the len() function do when used with an array in Go?
The len() function returns the number of elements in the array. It tells you how many items the array holds.
Click to reveal answer
beginner
How do you declare an array of 5 integers in Go?
You write: var arr [5]int. This creates an array named arr that can hold exactly 5 integers.
Click to reveal answer
beginner
If you have arr := [3]string{"a", "b", "c"}, what will len(arr) return?
It will return 3 because the array has 3 elements: "a", "b", and "c".
Click to reveal answer
beginner
Can the length of an array in Go change after it is created?
No, the length of an array in Go is fixed when you declare it. You cannot add or remove elements to change its size.
Click to reveal answer
intermediate
What is the difference between an array and a slice in Go regarding length?
An array has a fixed length known at compile time, while a slice is a flexible, dynamic view over an array and its length can change during runtime.
Click to reveal answer
What does len(arr) return if arr := [4]int{1, 2, 3, 4}?
A3
BUndefined
C0
D4
Can you change the length of an array after it is declared in Go?
ANo, arrays have fixed length
BYes, by using <code>len()</code>
CYes, by adding elements
DOnly if the array is empty
Which Go type allows flexible length that can change during runtime?
ASlice
BArray
CMap
DStruct
What is the output of len([3]string{"x", "y", "z"})?
A1
B3
C0
DError
How do you get the length of an array named numbers in Go?
A<code>numbers.length</code>
B<code>numbers.len()</code>
C<code>len(numbers)</code>
D<code>length(numbers)</code>
Explain how to find the length of an array in Go and why the length is fixed.
Think about how arrays are declared and how len() works.
You got /3 concepts.
    Describe the difference between an array and a slice in Go regarding their length and flexibility.
    Consider how each type handles size changes.
    You got /3 concepts.