The map m stores key-value pairs: "apple"=5 and "banana"=3.
Step 2: Calculate the sum of values
Adding m["apple"] (5) and m["banana"] (3) results in 8.
Final Answer:
8 -> Option B
Quick Check:
5 + 3 = 8 [OK]
Hint: Sum map values by accessing keys directly [OK]
Common Mistakes:
Concatenating values as strings instead of adding
Expecting zero for existing keys
Forgetting to initialize map before use
4. Identify the error in the following Go code snippet:
var m map[string]int
m["key"] = 10
fmt.Println(m["key"])
medium
A. No error, prints 10
B. Syntax error in map declaration
C. Key type must be int, not string
D. Map declared but not initialized, causes runtime panic
Solution
Step 1: Check map initialization
The map m is declared but not initialized with make, so it is nil.
Step 2: Understand runtime behavior
Assigning to a nil map causes a runtime panic (error) because the map is not ready to store values.
Final Answer:
Map declared but not initialized, causes runtime panic -> Option D
Quick Check:
Declare + make before use to avoid panic [OK]
Hint: Always initialize maps with make() before adding keys [OK]
Common Mistakes:
Forgetting to initialize map before assignment
Assuming declaration alone creates usable map
Confusing map declaration with slice declaration
5. Given a slice of strings keys := []string{"a", "b", "c"} and a slice of ints values := []int{1, 2, 3}, which code snippet correctly creates a map from keys to values?
hard
A. m := make(map[string]int)
for i := range keys {
m[keys[i]] = values[i]
}
B. m := map[string]int{}
for i, v := range values {
m[v] = keys[i]
}
C. m := make(map[int]string)
for i := 0; i < len(keys); i++ {
m[i] = keys[i]
}
D. m := make(map[string]int)
for i := 0; i < len(values); i++ {
m[values[i]] = keys[i]
}
Solution
Step 1: Match keys and values correctly
Keys are strings, values are ints. The map must have type map[string]int.
Step 2: Verify loop and assignment
m := make(map[string]int)
for i := range keys {
m[keys[i]] = values[i]
} loops over keys and assigns m[keys[i]] = values[i], correctly pairing keys to values.
Final Answer:
m := make(map[string]int)
for i := range keys {
m[keys[i]] = values[i]
} -> Option A
Quick Check:
Loop keys, assign values to map[key] [OK]
Hint: Loop keys, assign values to map[key] for correct pairing [OK]
Common Mistakes:
Swapping keys and values in map assignment
Using wrong map key or value types
Not initializing map before assignment
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.