📖 Scenario: You are organizing a small fruit basket. You want to keep track of the fruits you have and check specific fruits by their position in the basket.
🎯 Goal: Learn how to create an array of fruits and access specific fruits by their position (index) in the array.
📋 What You'll Learn
Create an array of fruits with exact values
Create a variable to hold the index of the fruit to access
Access the fruit at the given index from the array
Print the accessed fruit
💡 Why This Matters
🌍 Real World
Arrays are used to store collections of items like fruits, names, or numbers where you want to access items by their position.
💼 Career
Understanding how to access array elements is fundamental for programming tasks such as data processing, user input handling, and working with lists in software development.
Progress0 / 4 steps
1
Create an array of fruits
Create an array called fruits with these exact values: "Apple", "Banana", "Cherry", "Date", "Elderberry".
Go
Hint
Use the syntax varName := [size]type{values} to create an array.
2
Create an index variable
Create an integer variable called index and set it to 2 to select the third fruit in the array.
Go
Hint
Use variableName := value to create and set a variable.
3
Access the fruit at the given index
Create a variable called selectedFruit and set it to the fruit at position index in the fruits array.
Go
Hint
Use arrayName[index] to access an element at a specific position.
4
Print the selected fruit
Use fmt.Println to print the value of selectedFruit.
Go
Hint
Use fmt.Println(variableName) to print the value to the screen.
Practice
(1/5)
1. What is the index of the first element in a Go array?
easy
A. 1
B. Depends on the array size
C. -1
D. 0
Solution
Step 1: Understand Go array indexing
In Go, arrays start indexing from 0, meaning the first element is at index 0.
Step 2: Confirm the first element's index
The first element is always accessed with index 0, not 1 or any other number.
Final Answer:
0 -> Option D
Quick Check:
First element index = 0 [OK]
Hint: Remember, Go arrays start counting at zero [OK]
Common Mistakes:
Thinking indexing starts at 1
Assuming negative indexes are valid
Believing index depends on array size
2. Which of the following is the correct way to access the third element of an array named arr in Go?
easy
A. arr[3]
B. arr[2]
C. arr(2)
D. arr{2}
Solution
Step 1: Recall Go array indexing syntax
Go uses square brackets with zero-based indexing to access elements, so the third element is at index 2.
Step 2: Identify correct syntax
arr[2] accesses the third element; arr[3] accesses the fourth, arr(2) and arr{2} are invalid syntax.
Final Answer:
arr[2] -> Option B
Quick Check:
Third element = arr[2] [OK]
Hint: Use square brackets and zero-based index [OK]
Common Mistakes:
Using parentheses instead of brackets
Using 1-based index
Using curly braces for access
3. What will be the output of this Go code?
arr := [4]int{10, 20, 30, 40}
fmt.Println(arr[1])
medium
A. 10
B. 40
C. 20
D. 30
Solution
Step 1: Identify the array elements and their indexes
The array is [10, 20, 30, 40], indexed 0 to 3. Index 1 corresponds to the second element, 20.
Step 2: Determine the printed value
arr[1] accesses the element 20, so fmt.Println prints 20.
Final Answer:
20 -> Option C
Quick Check:
arr[1] = 20 [OK]
Hint: Remember index 1 is second element [OK]
Common Mistakes:
Printing arr[0] instead
Confusing index 1 with 2
Expecting output as 30 or 40
4. What is wrong with this Go code snippet?
arr := [3]int{1, 2, 3}
fmt.Println(arr[3])
medium
A. Index 3 is out of bounds for the array
B. Array declaration syntax is incorrect
C. fmt.Println cannot print array elements
D. The array size should be 4
Solution
Step 1: Check array size and valid indexes
The array has size 3, so valid indexes are 0, 1, and 2 only.
Step 2: Identify the error in accessing arr[3]
Accessing arr[3] tries to get the fourth element, which does not exist, causing an out-of-bounds runtime error.
Final Answer:
Index 3 is out of bounds for the array -> Option A
Quick Check:
Valid indexes: 0 to 2 only [OK]
Hint: Array indexes go from 0 to length-1 [OK]
Common Mistakes:
Using index equal to array length
Thinking fmt.Println can't print elements
Assuming array size auto-expands
5. Given the array arr := [5]int{5, 10, 15, 20, 25}, which code snippet correctly sums the first three elements?
hard
A. sum := arr[0] + arr[1] + arr[2]
B. sum := arr[1] + arr[2] + arr[3]
C. sum := arr[0] + arr[2] + arr[3]
D. sum := arr[2] + arr[3] + arr[4]
Solution
Step 1: Identify the first three elements
The first three elements are at indexes 0, 1, and 2, which are 5, 10, and 15 respectively.
Step 2: Check which option sums these elements
sum := arr[0] + arr[1] + arr[2] sums arr[0], arr[1], and arr[2], which is correct. Other options include elements beyond the first three.
Final Answer:
sum := arr[0] + arr[1] + arr[2] -> Option A
Quick Check:
Sum first three = arr[0]+arr[1]+arr[2] [OK]
Hint: Sum elements at indexes 0, 1, and 2 for first three [OK]
Common Mistakes:
Starting sum at index 1
Skipping elements
Including elements beyond first three
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.