Golang Maps Quiz
35 comprehensive questions on Golang maps, covering map creation, key existence checking, entry deletion, iteration order, and reference behavior — with 18 code examples demonstrating map operations and common patterns.
Question 1
What is a map in Golang?
Question 2
How do you create a map using make()?
Question 3
What is the difference between a nil map and an empty map?
Question 4
What happens when you assign to a nil map?
package main
func main() {
var m map[string]int
m["key"] = 1 // This line
}Question 5
How do you create a map with initial values?
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 2,
}
fmt.Println(m)
}Question 6
What is the comma ok idiom used for?
Question 7
What does this code print?
package main
import "fmt"
func main() {
m := map[string]int{"a": 0}
if v, ok := m["a"]; ok {
fmt.Println("Found:", v)
}
if v, ok := m["b"]; ok {
fmt.Println("Found:", v)
} else {
fmt.Println("Not found")
}
}Question 8
How do you delete an entry from a map?
Question 9
What happens when you delete a non-existent key?
package main
func main() {
m := make(map[string]int)
delete(m, "nonexistent") // This is safe
}Question 10
Is map iteration order guaranteed?
Question 11
What does this iteration code demonstrate?
package main
import "fmt"
func main() {
m := map[string]int{"c": 3, "a": 1, "b": 2}
for k, v := range m {
fmt.Printf("%s:%d ", k, v)
}
}Question 12
Are maps reference types?
Question 13
What happens when you pass a map to a function?
package main
import "fmt"
func modify(m map[string]int) {
m["new"] = 99
}
func main() {
m := make(map[string]int)
modify(m)
fmt.Println(len(m)) // 1
}Question 14
In a caching system, you're implementing an LRU cache using maps. Why is the randomized iteration order actually beneficial?
Question 15
What is the zero value of a map variable?
Question 16
How do you check if a map is empty?
package main
import "fmt"
func main() {
var m map[string]int
if len(m) == 0 {
fmt.Println("Empty")
}
}Question 17
What happens when you range over a nil map?
package main
func main() {
var m map[string]int
for k, v := range m {
// This is safe
}
}Question 18
Can map keys be of any type?
Question 19
What happens when you use a slice as a map key?
package main
func main() {
m := make(map[[]int]int) // This fails
}Question 20
In a configuration management system, you're storing user preferences in maps. If a user hasn't set a preference, how should you handle the zero value vs missing key distinction?
Question 21
What is map capacity and how does it work?
Question 22
How do you create a map that maps strings to functions?
package main
import "fmt"
func main() {
m := map[string]func() string{
"hello": func() string { return "Hello" },
}
fmt.Println(m["hello"]())
}Question 23
What does this code demonstrate about map assignment?
package main
import "fmt"
func main() {
m1 := map[string]int{"a": 1}
m2 := m1
m2["b"] = 2
fmt.Println(len(m1)) // 2
}Question 24
In a word frequency counter, you're using a map[string]int to count occurrences. Why is this efficient?
Question 25
What happens when you modify a map during iteration?
package main
import "fmt"
func main() {
m := map[string]int{"a": 1, "b": 2}
for k := range m {
if k == "a" {
delete(m, "b")
m["c"] = 3
}
fmt.Print(k, " ")
}
}Question 26
How do you implement a set using maps?
package main
import "fmt"
func main() {
set := make(map[string]bool)
set["apple"] = true
set["banana"] = true
if set["apple"] {
fmt.Println("Apple is in set")
}
}Question 27
What is the output of this nested map scenario?
package main
import "fmt"
func main() {
m := make(map[string]map[string]int)
m["users"] = make(map[string]int)
m["users"]["alice"] = 25
fmt.Println(m["users"]["alice"])
}Question 28
In a concurrent web server, multiple goroutines are updating a shared map of request counts. What synchronization is needed?
Question 29
What does the built-in len() function return for maps?
Question 30
How do you clear all entries from a map?
package main
func main() {
m := map[string]int{"a": 1, "b": 2}
// To clear:
for k := range m {
delete(m, k)
}
// or
m = make(map[string]int) // new map
}Question 31
What happens when you compare two maps?
package main
func main() {
m1 := map[string]int{"a": 1}
m2 := map[string]int{"a": 1}
if m1 == m2 { // This fails
// ...
}
}Question 32
In a JSON API, you're unmarshaling data into a map[string]interface{}. How do you safely access nested values?
Question 33
What is the performance characteristic of map operations?
Question 34
How do you iterate over maps in a deterministic order?
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{"c": 3, "a": 1, "b": 2}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Printf("%s:%d ", k, m[k])
}
}Question 35
What is the most important thing to remember about maps in concurrent programs?
