📖 Scenario: You are working on a simple inventory system for a small store. You need to keep track of the quantity of five different products.
🎯 Goal: Create an array to store the quantities of five products, set a threshold for low stock, find which products are below this threshold, and print their quantities.
📋 What You'll Learn
Create an array called quantities with exactly five integer values: 10, 3, 7, 2, 5
Create an integer variable called lowStockThreshold and set it to 4
Use a for loop with index i to check each quantity in quantities
Create a new array called lowStock to store quantities below the threshold
Print the lowStock array
💡 Why This Matters
🌍 Real World
Stores and warehouses often track product quantities to know when to reorder items.
💼 Career
Understanding arrays and loops is essential for managing collections of data in software development.
Progress0 / 4 steps
1
Create the quantities array
Create an array called quantities with these exact integer values: 10, 3, 7, 2, 5.
Go
Hint
Use Go's array syntax: varName := [size]type{values}
2
Set the low stock threshold
Create an integer variable called lowStockThreshold and set it to 4.
Go
Hint
Use variableName := value to create and set a variable.
3
Find quantities below the threshold
Use a for loop with index i to check each quantity in quantities. Create a slice called lowStock and append quantities less than lowStockThreshold to it.
Go
Hint
Use len(array) to get the array length and append(slice, value) to add items to a slice.
4
Print the low stock quantities
Write a fmt.Println statement to print the lowStock slice.
Go
Hint
Use fmt.Println(lowStock) to display the slice.
Practice
(1/5)
1. Which of the following correctly declares an array of 5 integers in Go?
easy
A. var arr int[5]
B. var arr [5]int
C. arr := []int{5}
D. arr := [5]int{1, 2, 3, 4}
Solution
Step 1: Understand Go array declaration syntax
In Go, arrays are declared with the syntax: var name [size]type.
Step 2: Check each option
A: var arr int[5] wrong syntax (type before size). B: arr := []int{5} is a slice, not an array. C: var arr [5]int correct syntax. D: arr := [5]int{1, 2, 3, 4} too few initializers (4 < 5), compilation error.
Final Answer:
var arr [5]int -> Option B
Quick Check:
Array declaration = var arr [5]int [OK]
Hint: Remember: var name [size]type declares an array [OK]
Common Mistakes:
Confusing slices with arrays
Using wrong syntax like int[5]
Missing var keyword for array declaration
2. Which of these is the correct way to initialize an array of 3 strings with values "a", "b", "c" in Go?
Arrays use [size]type{values} syntax. arr := [3]string{"a", "b", "c"} matches this exactly.
Step 2: Check other options for errors
arr := []string{"a", "b", "c"} is a slice, not an array. var arr [3]string = {"a", "b", "c"} uses invalid initialization with = and braces. arr := [3]string{"a", "b", c} misses quotes around "c" causing syntax error.
Final Answer:
arr := [3]string{"a", "b", "c"} -> Option A
Quick Check:
Array init with values = [3]string{"a", "b", "c"} [OK]
Hint: Use [size]type{values} for array initialization [OK]
When fewer elements are provided than the array size, Go fills remaining elements with zero values.
Step 2: Apply to given array
Array size is 4, initialized with 1 and 2. Remaining two elements become 0 by default.
Final Answer:
[1 2 0 0] -> Option A
Quick Check:
Partial init fills rest with zero [OK]
Hint: Uninitialized array elements default to zero [OK]
Common Mistakes:
Assuming array length matches initializer length
Expecting error for fewer initial values
Confusing slices with arrays
4. Identify the error in this Go array initialization:
var arr [3]int = [3]int{1, 2, 3, 4}
medium
A. No error, code is correct
B. Missing var keyword
C. Incorrect type declaration
D. Array size mismatch with initializer
Solution
Step 1: Check array size and initializer length
Array declared with size 3 but initializer has 4 elements.
Step 2: Understand Go array rules
Go requires initializer length to match array size exactly, else compilation error occurs.
Final Answer:
Array size mismatch with initializer -> Option D
Quick Check:
Initializer length must equal array size [OK]
Hint: Initializer length must match array size exactly [OK]
Common Mistakes:
Ignoring extra elements in initializer
Assuming Go auto-expands array size
Confusing slices with arrays
5. You want to create an array of 5 integers where the first two elements are 10 and 20, and the rest are zeros. Which code snippet correctly achieves this and prints the array?
hard
A. arr := [5]int{10, 20, 30}
fmt.Println(arr)
B. arr := [3]int{10, 20}
fmt.Println(arr)
C. arr := [5]int{10, 20, 0, 0, 0}
fmt.Println(arr)
D. arr := []int{10, 20}
fmt.Println(arr)
Solution
Step 1: Understand array initialization with explicit zeros
To ensure the last three elements are zeros explicitly, include them in the initializer.
Step 2: Check each option
B: arr := [3]int{10, 20}
fmt.Println(arr) wrong size 3, not 5. A: arr := [5]int{10, 20, 0, 0, 0}
fmt.Println(arr) explicitly sets zeros. C: arr := [5]int{10, 20, 30}
fmt.Println(arr) third element 30, not zero. D: arr := []int{10, 20}
fmt.Println(arr) is a slice, not array.
Step 3: Choose the best explicit solution
A clearly shows all elements matching the requirement exactly.