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}?✗ Incorrect
len() returns the number of elements in the array, which is 4 here.Can you change the length of an array after it is declared in Go?
✗ Incorrect
Arrays in Go have a fixed length that cannot be changed after declaration.
Which Go type allows flexible length that can change during runtime?
✗ Incorrect
Slices are dynamic and can grow or shrink, unlike arrays.
What is the output of
len([3]string{"x", "y", "z"})?✗ Incorrect
The array has 3 elements, so
len() returns 3.How do you get the length of an array named
numbers in Go?✗ Incorrect
The correct way is to use the built-in function
len().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.